Android 带有自定义布局和编辑文本的AlertDialog.Builder;无法访问视图
我正在尝试使用Android 带有自定义布局和编辑文本的AlertDialog.Builder;无法访问视图,android,Android,我正在尝试使用EditText对象创建警报对话框。我需要以编程方式设置EditText的初始文本。这是我的 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); // ...Irrelevant code for customizing the buttons and title AlertDialog alertDialog = dialogBuilder.create(); LayoutInflater infl
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_字段)代码>
不幸的是,这里还有另一个问题。显然,在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.findviewbyd(R.id.label_字段)
现在返回null
[/edit]editText
是alertDialog
布局的一部分,因此只需参考alertDialog
访问editText
EditText editText = (EditText) alertDialog.findViewById(R.id.label_field);
更新:
因为在代码行dialogBuilder.setView中(充气器.inflate(R.layout.alert_label_editor,null))代码>
充气机
为空
按照下面的方式更新代码,并尝试理解每个代码行的含义
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:
当您使用充气机创建的视图对象来更新UI组件时,您可以直接使用API 21及以后版本中的AlertDialog.Builder类的setView(int layourResId)
方法进行更改:
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();
EditText editText = (EditText) findViewById(R.id.label_field);
为此:
EditText editText = (EditText) v.findViewById(R.id.label_field);
用这个
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)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();
如果有人想在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()
重新发布答案。U也可以这样做:dialogBuilder.setView(R.layout.dialog\U布局)代码>@SiavA此方法仅在API 21中可用。我一直在尝试显示对话框,但它在RecyclerView中不起作用,但此方法起作用。当未定义充气机时,可以使用getLayoutInflater()
。@saigopi当覆盖onClick时,将有参数(DialogInterface Dialog,int id)。在这个onClick方法中,只需传递dialog.cancel();应该是builder.create().show()代码>,您可以检查builder.show()代码>代码了解更多详细信息
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()