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


101

我正在尝试使用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();

我需要更改什么才能拥有有效的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();

不幸的是,当您执行此操作时,alertDialog.findViewById(R.id.label_field);现在返回null

[/编辑]

Answers:


238

editTextalertDialog布局的一部分,因此只需访问以下editText内容即可alertDialog

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

更新:

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

inflaterNull

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

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

更新2:

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


22
您也可以按照以下方式进行操作:dialogBuilder.setView(R.layout.dialog_layout);
SiavA 2015年

4
@SiavA这种方法是可用的唯一形式API 21.
Scaraux

我一直在尝试显示Dialog,但它在RecyclerView中不起作用,但确实如此。
穆尼卜·米尔扎

您可以getLayoutInflater()inflater未定义时使用。
反斜杠

1
@saigopi当您覆盖onClick时,会有参数(DialogInterface对话框,int标识)。在此onClick方法中,只需传递dialog.cancel();即可。
Minkoo

28

用这个

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

应该是builder.create().show();,您可以查看builder.show();代码以了解更多详细信息
Phan Van Linh

9

你可以写:

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

3

如果有人在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()

转贴了@ user370305的答案。


2

更改此:

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

对此:

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

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

1
/**
 * 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");
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.