Android无法在AlertDialog上获取消息对象

时间:2013-02-12 01:00:41

标签: java android

我正在尝试在AlertDialog中设置消息的linktextcolor。但是,当我试图找到ViewById我的应用程序崩溃。我究竟做错了什么?我是否需要在XML中为活动提供消息?

final AlertDialog d =  new AlertDialog.Builder(new ContextThemeWrapper(SplashPage.this, R.style.Theme_Sherlock_Light_Dialog))
            .setIcon(android.R.drawable.ic_dialog_info).setTitle(getString(R.string.termsTitle))
                //.setView(message).setCancelable(false)
                .setMessage(Html.fromHtml(getString(R.string.terms))).setCancelable(false)
                .setPositiveButton(getString(R.string.accept), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        app.setTermsAccepted(true);
                        dialogInterface.dismiss();

                        Intent intent = new Intent(SplashPage.this, LoginPage.class);
                        startActivity(intent);
                    }
                }).create();

        //FAILING: TextView TV = (TextView)d.findViewById(android.R.id.message);
        //TV.setLinkTextColor(Color.MAGENTA);

2 个答案:

答案 0 :(得分:5)

我查看了AlertDialog文档,当您调用其方法时,它会搜索您在 onStart方法(http://developer.android.com/reference/android/app/Dialog.html#findViewById%28int%29)中处理的XML。相反,只需调用活动的 findViewById方法(例如,如果这是在活动类中,只需调用:

TextView TV = (TextView) findViewById(android.R.id.message);

应该有用。)

答案 1 :(得分:2)

如果您使用的是DialogFragment,则可以在DialogFragment.onStart()方法之后访问它,如前面的答案所示。

@Override
public void onStart() {
super.onStart();
    final TextView textView = (TextView) getDialog().findViewById(android.R.id.message);
    //Do something!
}