我一直在尝试在默认的 AlertDialog 中添加两个元素,但似乎无法使其正常工作。这是我的代码:
// START Dialog
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
TextView tv = new TextView(this);
tv.setText(title);
tv.setPadding(40, 40, 40, 40);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(20);
EditText et = new EditText(this);
etStr = et.getText().toString();
alertDialogBuilder.setView(et);
alertDialogBuilder.setTitle(title);
alertDialogBuilder.setMessage("Input Student ID");
alertDialogBuilder.setCustomTitle(tv);
if (isError)
alertDialogBuilder.setIcon(R.drawable.icon_warning);
// alertDialogBuilder.setMessage(message);
alertDialogBuilder.setCancelable(false);
// Setting Negative "Cancel" Button
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
});
// Setting Positive "Yes" Button
alertDialogBuilder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (isError)
finish();
else {
Intent intent = new Intent(
ChangeDeviceActivity.this,
MyPageActivity.class);
startActivity(intent);
}
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
try {
alertDialog.show();
} catch (Exception e) {
// WindowManager$BadTokenException will be caught and the app would
// not display the 'Force Close' message
e.printStackTrace();
}
目前,这只是一个EditText
带有消息设置的 a,alertDialogBuilder.setMessage("Input Student ID");
但我想将其设为 a TextView
,以便我可以居中证明它。我该怎么做呢?