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

Don*_*ert 91 android

我正在尝试使用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();
Run Code Online (Sandbox Code Playgroud)

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

[编辑]

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

不幸的是,这里有另一个问题.显然,设置内容视图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();
Run Code Online (Sandbox Code Playgroud)

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

[/编辑]

use*_*305 218

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

EditText editText = (EditText) alertDialog.findViewById(R.id.label_field);
Run Code Online (Sandbox Code Playgroud)

更新:

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

inflater空的.

更新您的代码,如下所示,并尝试了解每个代码行

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();
Run Code Online (Sandbox Code Playgroud)

  • 你也可以这样做:`dialogBu​​ilder.setView(R.layout.dialog_layout);` (22认同)
  • @SiavA此方法仅在API 21中可用。 (4认同)

小智 24

使用这个

   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();
Run Code Online (Sandbox Code Playgroud)


Sum*_*ena 7

你可以写:

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();
Run Code Online (Sandbox Code Playgroud)


Nil*_*kar 6

如果有人想在 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()
Run Code Online (Sandbox Code Playgroud)

转贴@ user370305的答案。