I need my app to test if the person clicks yes to continue or cancel to stop. I have a AlertDialogFragment:
public class FragmentConfirmarPicagem extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setTitle("Confirmar")
.setMessage("Deseja mesmo picar o ponto agora?")
.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.create();
}
}
And then I have this code in the MainActivity: new FragmentConfirmarPicagem().show(getSupportFragmentManager(), "Confirmar");
I know the onClick of positive/negative buttons have that function but like that in the MainActivity I can't know the clicked button.
Basically what I want is:
//if clicks Sim(Yes) does the following
switch (verificarPonto()) {
case 1:
txtHoraEntrada.setText(hora);
ponto.setEntrada(picagem);
return;
case 2:
txtHoraSaidaAlmoco.setText(hora);
ponto.setSaidaAlmoco(picagem);
return;
case 3:
txtHoraEntradaTarde.setText(hora);
ponto.setEntradaTarde(picagem);
return;
case 4:
txtHoraSaida.setText(hora);
ponto.setSaida(picagem);
return;
}
Why do you need to create a new class for a Dialog ?
You can do it directly from your activity
AlertDialog.Builder builder = new AlertDialog.Builder(thisActivity)
.setTitle("Confirmar")
.setMessage("Deseja mesmo picar o ponto agora?")
.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (verificarPonto()) {
case 1:
txtHoraEntrada.setText(hora);
ponto.setEntrada(picagem);
return;
...
}
}
})
.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
If for some reason you need to create a new class, you can do it like this
public static class FragmentConfirmarPicagem extends DialogFragment {
public DialogInterface.OnClickListener listener;
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setTitle("Confirmar")
.setMessage("Deseja mesmo picar o ponto agora?")
.setPositiveButton("Sim", listener)
.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.create();
}
}
then, in your activity
FragmentConfirmarPicagem fcp = new FragmentConfirmarPicagem();
fcp.listener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (verificarPonto()) {
case 1:
txtHoraEntrada.setText(hora);
ponto.setEntrada(picagem);
return;
...
}
}
};
fcp.show(getSupportFragmentManager(), "Confirmar");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.