You can create a simple dialog box by visiting this
Using dialogue box
-
28-09-2022 - |
Domanda
I am working on a game. I have used two progressbar as a battery on the players. Now I want to simply display a dialogue box when any of the progressbar get emptied. For decreasing the battery I have used the logic of counter which get decreased as collision is detected.
BattryLevelCPU.setProgress(counter--);
Now I want to use dialogue box to display when this counter==0
How I would be able to achieve it? I have tried out like
if counter==0{
//tried code of dialogue box
}
I haven't got any result except bunch of errors. Could I do something like this??
If counter==0 {
showDialog(DIALOG_ALERT);
}`@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_ALERT:
Builder builder = new AlertDialog.Builder(this);
builder.setMessage("This ends the activity");
builder.setCancelable(true);
builder.setPositiveButton("I agree", new OkOnClickListener());
builder.setNegativeButton("No, no", new CancelOnClickListener());
AlertDialog dialog = builder.create();
dialog.show();
}
return super.onCreateDialog(id);
}
private final class CancelOnClickListener implements
DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Cancle selected, activity continues",
Toast.LENGTH_LONG).show();
}
}
private final class OkOnClickListener implements
DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int which) {
AlertExampleActivity.this.finish();
}
} `
Soluzione
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow