Reputation: 133
I am trying to make a Alert Dialog , but the following code "setNegativeButton" and "setPositiveButton" become red , thats mean there are some error , what should I do ? Thanks!
`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();
Upvotes: 0
Views: 74
Reputation: 1076
Or this way :
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();
Upvotes: 0
Reputation: 2095
Do it this way:
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();
Upvotes: 0
Reputation: 2869
You must setPositiveButton not in AlertDialog but in 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();
Upvotes: 1