如何获得 AlertDialog 标题? [英] How to get the AlertDialog title?

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

问题描述

我尝试获取该消息并且以下代码行有效:

I tried to get the message and the following line of code works:

TextView dialogMessage = (TextView)dialogObject.findViewById(android.R.id.message);

但是当我尝试使用以下行获取标题时,它返回 null

But when I try to get the title using the following line it returns null

TextView dialogTitle = (TextView)dialogObject.findViewById(android.R.id.tittle);

推荐答案

我检查了 AlertDialog 的代码.在内部,他们使用 R.id.alertTitle 来初始化 AlertDialog 标题的 TextView.您可以使用 getIdentifier 来检索它:

I checked up the code of the AlertDialog. Internally they use R.id.alertTitle to initialize the AlertDialog title's TextView. You can use getIdentifier to retrieve it:

int titleId = getResources().getIdentifier( "alertTitle", "id", "android" );
if (titleId > 0) {
   TextView dialogTitle = (TextView) dialogObject.findViewById(titleId);
   if (dialogTitle != null) {

   }
}

对于AppCompatgetIdentifier的第三个参数应该是你的应用的包名.您可以使用 context.getPackageName()

for AppCompat, the third argument of getIdentifier should be the package name of your app. You can retrieve the latter with context.getPackageName()

这篇关于如何获得 AlertDialog 标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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