如何在 AlertDialog.Builder 上调用 findViewById?

How do I call findViewById on an AlertDialog.Builder?

我正在尝试使用以下代码在 AlertDialog 中获取对 TextView 的引用:

AlertDialog.Builder logoutBuilder = new AlertDialog.Builder(getActivity());      
TextView alertTextView = (TextView) logoutBuilder.findViewById(android.R.id.message);
alertTextView.setTextSize(40);

但我在 findViewById 处遇到编译器错误:

Cannot cast from AlertDialog.Builder to Dialog
The method findViewById(int) is undefined for the type AlertDialog.Builder

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

AlertDialog alert = builder.create();

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

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

当前接受的答案对我不起作用:它给了我一个空的 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。

我觉得很奇怪,接受的答案对我不起作用。它看起来与文档一致。尽管如此,我还是不得不这样处理。