我正在try 创建一个带有EditText
个对象的alert 对话框.我需要以编程方式设置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
对象?
[编辑]
因此,用户370305和其他人指出,我应该使用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
.
[/编辑]