AlertDialog.Builder with custom layout and EditText; cannot access view

我试图创建一个与 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对象?

[edit]

因此,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

[/编辑]

230535 次浏览

editText is a part of alertDialog layout so Just access editText with reference of alertDialog

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

更新:

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

inflater is 无效.

像下面这样更新您的代码,并尝试理解每一行代码

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:

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

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

Use this one

   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();
/**
* 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");
}

如果有人想在 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’s答案。

尝试制作视图 final

(定制版面)

final View layout=getLayoutInflater().inflate(R.layout.create_class,null);
builder.setView(layout);