Android 如何为在对话框中动态加载(setView)的布局获取元素(findViewById)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10313382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:15:19  来源:igfitidea点击:

How to get elements(findViewById) for a layout which is dynamically loaded(setView) in a dialog?

androidandroid-layoutandroid-edittextpreferenceactivity

提问by Vikas Singh

I need to get the EditText that's defined in an xml layout which is dynamically loaded as a view in a preference dialog i.e. :

我需要获取在 xml 布局中定义的 EditText,该布局在首选项对话框中作为视图动态加载,即:

public class ReportBugPreference extends EditTextPreference {

    @Override
    protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
        super.onPrepareDialogBuilder(builder);   
        builder.setView(LayoutInflater.from(ctx).inflate(R.layout.preference_report_bug_layout,null));
        EditText edttxtBugDesc = (EditText) findViewById(R.id.bug_description_edittext); // NOT WORKING
    }

}

EDIT : SOLUTIONby jjnFord

编辑:jjnFord 的解决方案

@Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
    super.onPrepareDialogBuilder(builder);  

    View viewBugReport = LayoutInflater.from(ctx).inflate(R.layout.preference_report_bug,null);
    EditText edttxtBugDesc = (EditText) viewBugReport.findViewById(R.id.bug_description_edittext);

    builder.setView(viewBugReport);



}

回答by jjNford

Since you are extending EditTextPreference you can just use the getEditText() method to grab the default text view. However, since you are setting your own layout this probably won't do what you are looking for.

由于您正在扩展 EditTextPreference,您可以只使用 getEditText() 方法来获取默认文本视图。但是,由于您正在设置自己的布局,因此这可能无法满足您的要求。

In your case you should Inflate your XML layout into a View object, then find the editText in the view - then you can pass your view to the builder. Haven't tried this, but just looking at your code I would think this is possible.

在您的情况下,您应该将 XML 布局膨胀为 View 对象,然后在视图中找到 editText - 然后您可以将您的视图传递给构建器。还没有尝试过这个,但只要看看你的代码,我就会认为这是可能的。

Something like this:

像这样的东西:

View view = (View) LayoutInflater.from(ctx).inflate(R.layout.preference_report_bug_layout, null);
EditText editText = view.findViewById(R.id.bug_description_edittext);
builder.setView(view);

回答by Sara

LayoutInflater is needed to create (or fill) View based on XML file in runtime. For example if you need to generate views dynamically for your ListView items. What is the layout inflater in an Android application?

LayoutInflater 需要在运行时基于 XML 文件创建(或填充)视图。例如,如果您需要为 ListView 项目动态生成视图。 Android 应用程序中的布局充气器是什么?

  1. Create your LayoutInflater:
  1. 创建您的 LayoutInflater:

LayoutInflater inflater = getActivity().getLayoutInflater();

LayoutInflater inflater = getActivity().getLayoutInflater();

  1. Create your view by inflater refered to your_xml_file:
  1. 通过参考 your_xml_file 的充气器创建您的视图:

View view= inflater.inflate(R.layout.your_xml_file, null);

View view= inflater.inflate(R.layout.your_xml_file, null);

  1. Find your object in your layout by id.
  1. 通过 id 在您的布局中找到您的对象。

TextView textView = (TextView)view.findViewById(R.id.text_view_id_in_your_xml_file);

TextView textView = (TextView)view.findViewById(R.id.text_view_id_in_your_xml_file);

  1. Use your object: i.e.
  1. 使用你的对象:即

textView.setText("Hello!");

textView.setText("Hello!");