如何在AlertDialog.Builder上调用findViewById? [英] How do I call findViewById on an AlertDialog.Builder?

查看:162
本文介绍了如何在AlertDialog.Builder上调用findViewById?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to get a reference to a TextView in the AlertDialog with this code:

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

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

But I'm getting a compiler error at findViewById:

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

推荐答案

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

Create the dialog from the AlertDialog.Builder, like so:

AlertDialog alert = builder.create();

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

Then, from the alert, you can invoke findViewById:

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

这篇关于如何在AlertDialog.Builder上调用findViewById?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆