1 回答
TA贡献2019条经验 获得超9个赞
为什么需要为 Dialog 创建一个新类?
你可以直接从你的活动中做到这一点
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();
如果由于某种原因你需要创建一个新类,你可以这样做
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();
}
}
然后,在你的活动中
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");
添加回答
举报