How to get the AlertDialog title?

后端 未结 5 857
無奈伤痛
無奈伤痛 2020-12-30 03:09

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

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


        
5条回答
  •  余生分开走
    2020-12-30 03:50

    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) {
    
       }
    }
    

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

提交回复
热议问题