How to get the AlertDialog title?

后端 未结 5 844
無奈伤痛
無奈伤痛 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:39

    To avoid the code from breaking, when Google decides to change its dialog title view id in the future, here is more reliable solution.

    We will simply return the first encountered View, which its type is TextView.

    //
    // Usage: findFirstEncounteredType(getDialog().getWindow().getDecorView(), TextView.class)
    //
    public static View findFirstEncounteredType(View view, Class klass) {
        if (klass.isInstance(view)) {
            return view;
        } else {
            if (!(view instanceof ViewGroup)) {
                return null;
            }
        }
    
        ViewGroup viewGroup = (ViewGroup)view;
    
        for (int i=0, ei=viewGroup.getChildCount(); i

提交回复
热议问题