puede implementar una solución genérica para decisiones y usarla en otro caso, no solo para sí / no, y personalizar la alerta con animaciones o diseño:
Algo como esto; Primero cree su clase para transferir datos:
public class AlertDecision {
private String question = "";
private String strNegative = "";
private String strPositive = "";
public AlertDecision question(@NonNull String question) {
this.question = question;
return this;
}
public AlertDecision ansPositive(@NonNull String strPositive) {
this.strPositive = strPositive;
return this;
}
public AlertDecision ansNegative(@NonNull String strNegative) {
this.strNegative = strNegative;
return this;
}
public String getQuestion() {
return question;
}
public String getAnswerNegative() {
return strNegative;
}
public String getAnswerPositive() {
return strPositive;
}
}
después de una interfaz para devolver el resultado
public interface OnAlertDecisionClickListener {
/**
* Interface definition for a callback to be invoked when a view is clicked.
*
* @param dialog the dialog that was clicked
* @param object The object in the position of the view
*/
void onPositiveDecisionClick(DialogInterface dialog, Object object);
void onNegativeDecisionClick(DialogInterface dialog, Object object);
}
Ahora puede crear utilidades para acceder fácilmente (en esta clase puede implementar diferentes animaciones o diseños personalizados para la alerta):
public class AlertViewUtils {
public static void showAlertDecision(Context context,
@NonNull AlertDecision decision,
final OnAlertDecisionClickListener listener,
final Object object) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(decision.getQuestion());
builder.setPositiveButton(decision.getAnswerPositive(),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
listener.onPositiveDecisionClick(dialog, object);
}
});
builder.setNegativeButton(decision.getAnswerNegative(),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
listener.onNegativeDecisionClick(dialog, object);
}
});
android.support.v7.app.AlertDialog dialog = builder.create();
dialog.show();
}
}
y la última llamada en actividad o fragmento; puede usar esto en su caso o para otra tarea:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
initResources();
}
public void initResources() {
Button doSomething = (Button) findViewById(R.id.btn);
doSomething.setOnClickListener(getDecisionListener());
}
private View.OnClickListener getDecisionListener() {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDecision decision = new AlertDecision()
.question("question ...")
.ansNegative("negative action...")
.ansPositive("positive action... ");
AlertViewUtils.showAlertDecision(MainActivity.this,
decision, getOnDecisionListener(), v);
}
};
}
private OnAlertDecisionClickListener getOnDecisionListener() {
return new OnAlertDecisionClickListener() {
@Override
public void onPositiveDecisionClick(DialogInterface dialog, Object object) {
//do something like create, show views, etc...
}
@Override
public void onNegativeDecisionClick(DialogInterface dialog, Object object) {
//do something like delete, close session, etc ...
}
};
}
}