Issue
So i have tis code and i'm trying to create a AlertDialog with an EditTet and Three buttons the positive one, the négative one and the neutral one , but it doesn't work and the application stops
b5.setOnClickListener(new View.OnClickListener() {
@SuppressLint("UseCompatLoadingForDrawables")
@Override
public void onClick(View view) {
AlertDialog.Builder boite;
boite = new AlertDialog.Builder(MainActivity.this);
boite.setTitle("boite de dialogue");
boite.setIcon(getDrawable(R.drawable.warning_shield_96px));
final EditText input = new EditText(MainActivity.this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
boite.setView(input);
boite.setPositiveButton("OUI", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//whatever action
}
});
boite.show();
boite.setNegativeButton("NON", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//whatever action
}
});
boite.show();
boite.setNeutralButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//whatever action
}
});
boite.show();
}
});
Solution
There is no need to call boite.show() several times, just call it once like below :
b5.setOnClickListener(new View.OnClickListener() {
@SuppressLint("UseCompatLoadingForDrawables")
@Override
public void onClick(View view) {
AlertDialog.Builder boite;
boite = new AlertDialog.Builder(MainActivity.this);
boite.setTitle("boite de dialogue");
boite.setIcon(getDrawable(R.drawable.warning_shield_96px));
final EditText input = new EditText(MainActivity.this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
boite.setView(input);
boite.setPositiveButton("OUI", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//whatever action
}
});
boite.setNegativeButton("NON", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//whatever action
}
});
boite.setNeutralButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//whatever action
}
});
boite.show();
}
});
AlertDialog uses Builder Pattern to initialize, so you can set different methods and buttons and anything you like, then when you call alertDialog.show() it builds the object with any configs you set before that call.
Answered By - Yamin Yazdanpanah
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.