如何获得元素(findViewById),这些动态在对话框中加载(的setView)的布局? [英] How to get elements(findViewById) for a layout which is dynamically loaded(setView) in a dialog?

查看:243
本文介绍了如何获得元素(findViewById),这些动态在对话框中加载(的setView)的布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要得到多数民众赞成在其中动态加载在preference对话框即一个视图中的XML布局定义的EditText:

 公共类reportbug的preference延伸的EditText preference {

    @覆盖
    在prepareDialogBu​​ilder(AlertDialog.Builder制造商)保护无效{
        super.on prepareDialogBu​​ilder(制造商);
        builder.setView(LayoutInflater.from(CTX).inflate(R.layout preference_report_bug_layout,NULL));
        EditText上edttxtBugDesc =(EditText上)findViewById(R.id.bug_description_edittext); //不工作
    }

}
 

编辑:SOLUTION 按jjnFord

  @覆盖
在prepareDialogBu​​ilder(AlertDialog.Builder制造商)保护无效{
    super.on prepareDialogBu​​ilder(制造商);

    查看viewBugReport = LayoutInflater.from(CTX).inflate(R.layout preference_report_bug,空);
    EditText上edttxtBugDesc =(EditText上)viewBugReport.findViewById(R.id.bug_description_edittext);

    builder.setView(viewBugReport);



}
 

解决方案

由于要扩展的EditText preference你可以使用getEditText()方法来获取默认的文本视图。不过,既然你要设置自己的布局这可能不会做你所期待的。

在你的情况,你应该充气的XML布局到一个视图对象,然后找到EDITTEXT视图 - 那么你可以通过你的观点的建设者。没有试过,只是看你的code我认为这是可能的。

事情是这样的:

 查看视图=(查看)LayoutInflater.from(CTX).inflate(R.layout preference_report_bug_layout,空);
EditText上EDITTEXT = view.findViewById(R.id.bug_description_edittext);
builder.setView(视图);
 

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. :

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 : SOLUTION by 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);



}

解决方案

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.

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.

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);

这篇关于如何获得元素(findViewById),这些动态在对话框中加载(的setView)的布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆