我正在尝试制作警报对话框,但是以下代码" setNegativeButton"和" setPositiveButton"变成红色,这意味着有一些错误, 我该怎么办 ? 谢谢!
`enter code here`AlertDialog alertDialog = new AlertDialog.Builder(GameActivity.this).create();
alertDialog.setTitle("Game Over!");
alertDialog.setMessage(" Total time " + String.valueOf(timeSpent));
alertDialog.setPositiveButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.setNegativeButton("Restart", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
答案 0 :(得分:1)
你必须setPositiveButton不在AlertDialog中但在AllertDialog.Builder()中。
AlertDialog alertDialog = new AlertDialog.Builder(GameActivity.this)
.setTitle("Game Over!")
.setMessage(" Total time " + String.valueOf(timeSpent))
.setPositiveButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("Restart", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create();
alertDialog.show();
答案 1 :(得分:0)
这样做:
AlertDialog alertDialog = new AlertDialog.Builder(GameActivity.this).create();
alertDialog.setTitle("Game Over!");
alertDialog.setMessage(" Total time " + String.valueOf(timeSpent));
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE,"Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Restart", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
答案 2 :(得分:0)
或者这样:
AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setTitle("TITLE");
ad.setCancelable(true);
ad.setMessage("MESSAGE");
ad.setPositiveButton(getResources().getString(R.string.ok), new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
} });
ad.show();