如何获取AlertDialog标题



我试图获得消息,下面的代码行有效:

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

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

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

我检查了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) {
   }
}

编辑:对于AppCompatgetIdentifier的第三个参数应该是应用程序的包名称。您可以使用context.getPackageName() 检索后者

为了避免代码中断,当谷歌决定在未来更改其对话框标题视图id时,这里有一个更可靠的解决方案。

我们将简单地返回第一个遇到的View,其类型为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;
}

我知道这个问题提到AlertDialog,但如果你通过谷歌搜索来这里寻找DialogFragment:的答案

getDialog().findViewById(android.R.id.title))

您可以自己实现它。创建自己的类来扩展android.app.AlertDialog.Builder。然后在使用方法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;
    }
}

然后使用您可以将其用作普通的AlertDialog,并实现获取其标题的方法。然后你可以测试它:

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();
    }
}

我知道这是一篇旧文章,但我使用了已接受的答案,并添加了其他对我有用的内容。您可以使用下面的代码使对话框标题多行(默认为1行)。我还读出了预设标题,因为我稍后使用ContentLoaders 异步添加了txt

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 + ":nn" + "next line txt";
                dialogTitle.setText(txt1);
            }
        }

最新更新