Alertdialog setView()使用现有的xml小部件

时间:2015-08-13 11:36:53

标签: java android

我在带有LinearLayout的.xml文件中有一个EditText,并希望在AlertDialog上将其添加为setView()参数。这可能吗?这是我尝试过的:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText input = new EditText(this);
builder.setView(input);

但是启动时对话框是空白的。我做错了什么?

2 个答案:

答案 0 :(得分:0)

像这样使用

AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.your_layout, null);
builder.setView(dialogView);

AlertDialog alertDialog = builder.create();
alertDialog.show();

使用dialogView,您可以获得EditText这样的

EditText editText = (EditText) dialogView.findViewById(R.id.your_edit_text);

答案 1 :(得分:0)

这可能会有所帮助

 AlertDialog.Builder builder = new AlertDialog.Builder(this);
   LayoutInflater inflater = getLayoutInflater();
builder.setView(inflater.inflate(R.layout.YourLayout, null));
AlertDialog ad = builder.create();