[英]Android can't get message object on AlertDialog
我正在嘗試在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);
我看着AlertDialog文件,看來,當你調用它的方法,它會搜索你已經在 OnStart方法處理的XML( http://developer.android.com/reference/android/app/Dialog.html# findViewById%28int%29 )。 相反,只需調用您的activity的 findViewById方法(例如,如果這是在一個活動類中,只需調用:
TextView TV = (TextView) findViewById(android.R.id.message);
應該管用。)
如果您使用的是DialogFragment,則可以在DialogFragment.onStart()方法之后訪問它,如前面的答案所示。
@Override
public void onStart() {
super.onStart();
final TextView textView = (TextView) getDialog().findViewById(android.R.id.message);
//Do something!
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.