Android AlertDialog.Builder 具有自定义布局和 EditText;无法访问视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22655599/
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 06:18:23  来源:igfitidea点击:

AlertDialog.Builder with custom layout and EditText; cannot access view

android

提问by Don Subert

I am trying to create an alert dialog with an EditTextobject. I need to set the initial text of the EditTextprogrammatically. Here's what I have.

我正在尝试创建一个带有EditText对象的警报对话框。我需要以EditText编程方式设置初始文本。这就是我所拥有的。

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// ...Irrelevant code for customizing the buttons and title
AlertDialog alertDialog = dialogBuilder.create();
LayoutInflater inflater = this.getLayoutInflater();
alertDialog.setContentView(inflater.inflate(R.layout.alert_label_editor, null));
EditText editText = (EditText) findViewById(R.id.label_field);
editText.setText("test label");
alertDialog.show();

What do I need to change so that I can have a valid EditTextobject?

我需要改变什么才能拥有一个有效的EditText对象?

[edit]

[编辑]

So, it was pointed out by user370305 and others that I should be using alertDialog.findViewById(R.id.label_field);

所以,user370305 和其他人指出我应该使用 alertDialog.findViewById(R.id.label_field);

Unfortunately there is another issue here. Apparently, setting the content view on the AlertDialogcauses the program to crash at runtime. You have to set it on the builder.

不幸的是,这里还有另一个问题。显然,设置内容视图AlertDialog会导致程序在运行时崩溃。您必须在构建器上设置它。

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// ...Irrelevant code for customizing the buttons and title
dialogBuilder.setView(inflater.inflate(R.layout.alert_label_editor, null));
AlertDialog alertDialog = dialogBuilder.create();
LayoutInflater inflater = this.getLayoutInflater();
EditText editText = (EditText) alertDialog.findViewById(R.id.label_field);
editText.setText("test label");
alertDialog.show();

Unfortunately, when you do this, alertDialog.findViewById(R.id.label_field);now returns null.

不幸的是,当你这样做时,alertDialog.findViewById(R.id.label_field);现在返回null.

[/edit]

[/编辑]

回答by user370305

editTextis a part of alertDialoglayout so Just access editTextwith reference of alertDialog

editTextalertDialog布局的一部分,所以只需editText参考alertDialog

EditText editText = (EditText) alertDialog.findViewById(R.id.label_field);

Update:

更新:

Because in code line dialogBuilder.setView(inflater.inflate(R.layout.alert_label_editor, null));

因为在代码行中 dialogBuilder.setView(inflater.inflate(R.layout.alert_label_editor, null));

inflateris Null.

inflaterNull

update your code like below, and try to understand the each code line

像下面这样更新您的代码,并尝试理解每个代码行

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// ...Irrelevant code for customizing the buttons and title
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
dialogBuilder.setView(dialogView);

EditText editText = (EditText) dialogView.findViewById(R.id.label_field);
editText.setText("test label");
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();

Update 2:

更新 2:

As you are using View object created by Inflater to update UI components else you can directly use setView(int layourResId)method of AlertDialog.Builderclass, which is available from API 21 and onwards.

当您使用 Inflater 创建的 View 对象来更新 UI 组件时,您可以直接使用类的setView(int layourResId)方法AlertDialog.Builder,该方法可从 API 21 及以后使用。

回答by Naveen Kumar

Use this one

用这个

   AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    // Get the layout inflater
    LayoutInflater inflater = (activity).getLayoutInflater();
    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the
    // dialog layout
    builder.setTitle(title);
    builder.setCancelable(false);
    builder.setIcon(R.drawable.galleryalart);
    builder.setView(inflater.inflate(R.layout.dialogue, null))
    // Add action buttons
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {

                    }
                }
            });
    builder.create();
    builder.show();

回答by Sumit Saxena

You can write:

你可以写:

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

 // ...Irrelevant code for customizing the buttons and title

LayoutInflater inflater = this.getLayoutInflater(); 

View dialogView= inflater.inflate(R.layout.alert_label_editor, null);                    
dialogBuilder.setView(dialogView);

Button button = (Button)dialogView.findViewById(R.id.btnName);

   button.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {

         //Commond here......

       }
   });

EditText editText = (EditText)
dialogView.findViewById(R.id.label_field); 

editText.setText("test label"); 

dialogBuilder.create().show();

回答by Nilesh Deokar

In case any one wants it in Kotlin :

如果有人想在 Kotlin 中使用它:

val dialogBuilder = AlertDialog.Builder(this)
// ...Irrelevant code for customizing the buttons and title
val dialogView = layoutInflater.inflate(R.layout.alert_label_editor, null)
dialogBuilder.setView(dialogView)

val editText =  dialogView.findViewById(R.id.label_field)
editText.setText("test label")
val alertDialog = dialogBuilder.create()
alertDialog.show()

Reposted @user370305'sanswer.

转贴@ user370305的答案。

回答by The Holy Coder

Change this:

改变这个:

EditText editText = (EditText) findViewById(R.id.label_field);

to this:

对此:

EditText editText = (EditText)  v.findViewById(R.id.label_field);

回答by Sercan Ozdemir

View v=inflater.inflate(R.layout.alert_label_editor, null);
alertDialog.setContentView(v);
EditText editText = (EditText)v.findViewById(R.id.label_field);
editText.setText("test label");
alertDialog.show();

回答by CodeToLife

/**
 * Shows  confirmation dialog about signing in.
 */
private void startAuthDialog() {
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.show();

    alertDialog.getWindow().setLayout(800, 1400);
    LayoutInflater inflater = this.getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.auth_dialog, null);
    alertDialog.getWindow().setContentView(dialogView);
    EditText editText = (EditText) dialogView.findViewById(R.id.label_field);
    editText.setText("test label");
}