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

我正在尝试使用EditText对象创build一个警告对话框。 我需要以编程方式设置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上设置内容视图会导致程序在运行时崩溃。 你必须把它放在build造者身上。

 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。

[/编辑]

editTextalertDialog布局的一部分,所以只需通过editText的引用访问alertDialog

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

更新:

因为在代码行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(); 

使用这个

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

你可以写:

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

改变这个:

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

对此:

 EditText editText = (EditText) v.findViewById(R.id.label_field); 
 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();