Ceci est mon code pour une alerte, il affiche un message lorsque vous appuyez sur le bouton. Comment puis-je créer un bouton OK pour rejeter?
Button bm1 = (Button) findViewById(R.id.button1);
bm1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog alert = new AlertDialog.Builder(screen4.this).create();
alert.setTitle("Doctor");
alert.setMessage("message");
alert.setCancelMessage(null);
alert.show();
}
});
}}
J'ai mis alert.setCancelMessage (null) mais il n'affiche aucun bouton à ignorer.
Merci pour toutes les réponses.
Solution:
Button bm1 = (Button) findViewById(R.id.button1);
bm1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Builder alert = new AlertDialog.Builder(screen4.this);
alert.setTitle("Doctor");
alert.setMessage("message");
alert.setPositiveButton("OK", null);
alert.show();
}
});
Modifiez votre dialogue d'alerte en ceci:
Builder alert = new AlertDialog.Builder(screen4.this);
alert.setTitle("Doctor");
alert.setMessage("message");
alert.setPositiveButton("OK",null);
alert.show();
Vous pouvez créer à la fois le bouton OK et le bouton Annuler pour la boîte de dialogue en utilisant ceci,
AlertDialog.Builder builder = new AlertDialog.Builder(
getApplicationContext());
builder.setCancelable(true);
builder.setTitle("Title");
builder.setInverseBackgroundForced(true);
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
Cela ne pourrait-il pas être fait en utilisant uniquement un bouton positif?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Look at this dialog!")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
}
});
AlertDialog alert = builder.create();
alert.show();
Vous pouvez le faire en utilisant setPositiveButton
et setNegativeButton
alert.setPositiveButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface alert, int which) {
// TODO Auto-generated method stub
//Do something
alert.dismiss();
}
});
alert.setNegativeButton("Cancel", new OnClickListener() {
@Override
public void onClick(DialogInterface alert, int which) {
// TODO Auto-generated method stub
alert.dismiss();
}
});
Édité:
alert.setButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
// Do something
alert.dismiss();
}
});
alert.setButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
alert.dismiss();
}
});
Vérifiez la page AlertDialog.Builder dans l'API: http://developer.Android.com/reference/Android/app/AlertDialog.Builder.html
Vous pouvez définir des boutons positifs, neutres et négatifs à l'aide des méthodes setPositiveButton, setNeutralButton et setNegativeButton, respectivement
Button bm1 = (Button) findViewById(R.id.button1);
bm1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final AlertDialog dialog = new AlertDialog.Builder(screen4.this).create();
dialog.setTitle("Doctor");
dialog.setMessage("message");
dialog.setButton("Ok",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// here you can add functions
}
});
dialog.setButton2("Cancel",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// here you can add functions
dialog.dismiss();
}
});
dialog.show();
}};
bt = (Button) findViewById(R.id.button1);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ab = new AlertDialog.Builder(MainActivity.this);
ab.setTitle("confirm");
ab.setIcon(R.drawable.ic_launcher);
ab.setMessage("Do u want to close this application?");
ab.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
Toast.makeText(MainActivity.this, "ok", Toast.LENGTH_SHORT).show();
}
});
ab.setNegativeButton("cancle", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
Toast.makeText(MainActivity.this, "cancle", Toast.LENGTH_SHORT).show();
}
});
ab.show();
}
});
alert.setPositiveButton ou alert.setNegativeButton
Button bm1 = (Button) findViewById(R.id.button1);
bm1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog alert = new AlertDialog.Builder(screen4.this).create();
alert.setTitle("Doctor");
alert.setMessage("message");
alert.setCancelMessage(null);
alert.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//do somthing or dismiss dialog by dialog.dismiss();
}
});
alert.show();
}
});
}}