How to get the AlertDialog title?

2019-02-08 16:03发布

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

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

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

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

5条回答
等我变得足够好
2楼-- · 2019-02-08 16:36

You can implement it your self. Create your own class that extend android.app.AlertDialog.Builder. And then create a variable to store your value after using the method setTitle().


import android.content.Context;
import android.support.annotation.StringRes;

public class AlertDialog extends android.app.AlertDialog.Builder {

    private Context context;
    private String title;

    public AlertDialog(Context context) {
        super(context);
        this.context = context;
    }

    public AlertDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    /**
     * Set title using string
     */
    @Override
    public AlertDialog setTitle(CharSequence title) {

        // set the title
        this.title = title.toString();

        super.setTitle(title);
        return this;
    }

    /**
     * Set title using resource string
     */
    @Override
    public AlertDialog setTitle(@StringRes int titleId) {

        this.title = this.context.getText(titleId).toString();

        super.setTitle(titleId);
        return this;
    }

    // create public method
    public String getTitle(){
        return this.title;
    }
}

And then use you can use it as ordinary AlertDialog, with implemented method which get its title. Then you can test it:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Crete new alert dialog
        AlertDialog dialog = new AlertDialog(this);
        dialog.setMessage("Type some message here :D");
        dialog.setTitle(R.string.settings_title);           // string resource
        dialog.setTitle("Dialog1 title");                   // string
        dialog.show();

        // Crete secondary dialog with same title
        // using getTitle() method
        new AlertDialog(this)
                .setTitle(dialog.getTitle())
                .setMessage("Copy title from first dialog.")
                .show();

    }
}
查看更多
闹够了就滚
3楼-- · 2019-02-08 16:39

I know this is an old post but I used the accepted answer and added something else useful to me. You can make the Dialog title multiline (default 1 line) using the code below. I also read out the preset title because I added txt asynchronously later on using ContentLoaders

int titleId = getResources().getIdentifier( "alertTitle", "id", "android" );
        if (titleId > 0) {
            TextView dialogTitle = (TextView) getDialog().findViewById(titleId);
            if (dialogTitle != null) {
                dialogTitle.setMaxLines(4);
                dialogTitle.setSingleLine(false);
                String txt = dialogTitle.getText().toString();
                String txt1 = txt + ":\n\n" + "next line txt";
                dialogTitle.setText(txt1);
            }
        }
查看更多
家丑人穷心不美
4楼-- · 2019-02-08 16:40

I know the question mentions AlertDialog, but if you came here through a Google search and looking for the answer for a DialogFragment:

getDialog().findViewById(android.R.id.title))
查看更多
Anthone
5楼-- · 2019-02-08 16:46

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<? extends View> 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<ei; i++) {
        View child = viewGroup.getChildAt(i);
        View result = findFirstEncounteredType(child, klass);
        if (result != null) {
            return result;
        }
    }

    return null;
}
查看更多
Emotional °昔
6楼-- · 2019-02-08 16:48

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

   }
}
查看更多
登录 后发表回答