如何在AlertDialog.Builder上调用findViewById?

时间:2016-03-04 23:48:51

标签: android alertdialog

我尝试使用以下代码获取<span class="icones glyphicon glyphicon-pen">TextView的引用:

AlertDialog

但是我在AlertDialog.Builder logoutBuilder = new AlertDialog.Builder(getActivity()); TextView alertTextView = (TextView) logoutBuilder.findViewById(android.R.id.message); alertTextView.setTextSize(40); 收到编译错误:

findViewById

2 个答案:

答案 0 :(得分:16)

从AlertDialog.Builder创建对话框,如下所示:

AlertDialog alert = builder.create();

然后,从警报中,您可以调用findViewById:

TextView alertTextView = (TextView) alert.findViewById(android.R.id.message);
alertTextView.setTextSize(40);

答案 1 :(得分:0)

当前接受的答案对我不起作用:它为我提供了一个空的TextView对象。相反,我需要从我夸大的视图中调用findViewById

AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
View v = getLayoutInflater().inflate(R.layout.view_dialog, null);
builder.setView(v);

现在,我可以使用v的findViewById找到TextView:

TextView card_info = v.findViewById(R.id.view_card_info_card_num);

稍后,我打电话给builder.show()以显示AlertDialog。

我发现接受的答案对我不起作用很奇怪。它似乎与文档一致。但是,我必须以这种方式进行处理。

相关问题