Я использую AlertDialog () с двумя кнопками;после щелчка AlertDialog должен вернуть значение.Я написал код ниже, но я не могу понять поведение AlertDialog.Вот мой код:
Моя функция TransactionAccDialog () должна возвращать либо true, либо false, в зависимости от выбора пользователя (нажатие кнопки).В зависимости от возвращаемого значения будут выполняться другие действия.
private boolean TransactionAccDialog() {
final boolean[] answer = new boolean[1];
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Information");
builder.setMessage("Transaction accéptée");
builder.setPositiveButton(" Confirmer ", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.i("lamyae", "input" + input);
answer[0]=true;
dialog.dismiss();
}
});
builder.setNeutralButton(" Annuler ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
answer[0]=false;
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
Button negativeButton = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);
positiveButton.setTextColor(Color.parseColor("#FFFFFFFF"));
positiveButton.setBackgroundColor(Color.parseColor("#FF00FF00"));
negativeButton.setTextColor(Color.parseColor("#FFFFFFFF"));
negativeButton.setBackgroundColor(Color.parseColor("#FFFF0000"));
}
});
return answer[0];
}
//The return values are used as follow in the same Fragment as the alertdialog:
public void user(){
if(TransactionAccDialog()==true){
ImpresssionDialog();
ImpressionTicket();
}
else {
try {
getActivity().finish();
Intent intent = new Intent(getActivity(), CashActivity1.class);
startActivity(intent);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
Я ожидаю, что alerttdialog подождет действия, а затем продолжит.Но дело не в этом. Это напрямую связано с выполнением фрагмента кода "else {}".
Может кто-нибудь помочь.Мне нужен способ подождать, пока не будет выполнено действие с одной из кнопок.