Comment porter un toast au-dessus d'un AlertDialog?
-
23-09-2019 - |
Question
Je suis un AlertDialog validais, et je voudrais porter un toast au-dessus de l'écran AlertDialog.
J'ai ce code, mais le Toast est affiché sur l'activité
new AlertDialog.Builder(this).setTitle(R.string.contact_groups_add)
.setView(addView).setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
if (wrapper.getTitle().length()>0)
{
processAdd(wrapper);
} else {
Toast.makeText(getApplicationContext(), "Name is required", Toast.LENGTH_SHORT).show();
}
}
}).setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// ignore, just dismiss
}
}).show();
Autres conseils
eu ce problème moi aussi, quand je voulais montrer un message de validation au sein d'une boîte de dialogue. La réponse que seanhodges a donné est probablement le plus propre et mieux. Mais une activité séparée était pas moi pratique pour, donc je suis venu avec cette solution.
Quoi qu'il en soit, vous pouvez utiliser le AlerDialog.Builder, et montrer un toast. Si vous remplacez le OnClickListener du bouton le vous voulez déclencher le pain grillé, vous pouvez afficher un toast au-dessus d'une boîte de dialogue.
Un exemple;
public void showToastOnDialog(final Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Dialog title");
builder.setMessage("Dialog message");
builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing, you will be overriding this anyway
}
});
builder.setNegativeButton(android.R.string.cancel,
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// You can implement code here, because you wont be
// overriding this
}
});
final AlertDialog dialog = builder.create();
// Make sure you show the dialog first before overriding the
// OnClickListener
dialog.show();
// Notice that I`m not using DialogInterface.OnClicklistener but the
// View.OnClickListener
dialog.getButton(Dialog.BUTTON_POSITIVE).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast toast = Toast.makeText(context,
"I`m a toast on top of a dialog.",
Toast.LENGTH_LONG);
toast.show();
// Because you are overriding the OnClicklistener, the
// dialog will not auto dismiss
// after clicking
dialog.dismiss();
}
});
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow