如何获得AlertDialog标题?

ali*_*tha 15 testing android dialog

我试图获取消息,以下代码行有效:

TextView dialogMessage = (TextView)dialogObject.findViewById(android.R.id.message);
Run Code Online (Sandbox Code Playgroud)

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

TextView dialogTitle = (TextView)dialogObject.findViewById(android.R.id.tittle);
Run Code Online (Sandbox Code Playgroud)

Bla*_*elt 27

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

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

   }
}
Run Code Online (Sandbox Code Playgroud)

  • 对于AppCompat AlertDialog,您应该使用getResources().getIdentifier("alertTitle","id","your.package.name") (5认同)
  • 尽管人们说不可能,但这是一个很好的答案.你值得拥有黑带,非常感谢. (2认同)