DialogFragment timepicker onCancel和onDismiss问题
DialogFragment timepicker onCancel and onDismiss problem
我已经尝试实现一个由 SwitchCompat 打开的时间选择器,所以当我启用该选项时,时间选择器会出现,它工作正常但如果我按下取消或后退按钮或简单地按下在时间选择器之外,开关保持在打开位置,当我不在时间选择器中选择时间时,我希望它回到关闭状态。以为 onCancel 方法可能有帮助,但没有,或者只是我在实现它时犯了一些错误,有相关代码:
public class Settings extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener, TimePickerDialog.OnDismissListener, TimePickerDialog.OnCancelListener {
private EditText textLastHour;
private KeyListener keyListener;
private SwitchCompat hourSwitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
textLastHour = findViewById(R.id.finalHourText);
textLastHour.setKeyListener(null);
textLastHour.setHint("Option not selected");
hourSwitch = findViewById(R.id.switch_hour);
hourSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
DialogFragment timePicker = new TimePickerFragment();
timePicker.show(getSupportFragmentManager(), "time picker");
} else {
textLastHour.setText("");
}
}
});
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
textLastHour = findViewById(R.id.finalHourText);
textLastHour.setText("Final alarm at: " + hourOfDay + ":" + minute);
}
@Override
public void onCancel(DialogInterface dialog) {
hourSwitch.setChecked(false);
}
@Override
public void onDismiss(DialogInterface dialog) {
hourSwitch.setChecked(false);
}
}
onDismiss
和onCancel
我都试过了,这里都不行,请问取消Time Picker后,有没有办法让Switch回到原来的位置?将 DatePicker
添加到标签中,因为我觉得它可能与 TimePicker
对于该问题具有相同的解决方案。
我的TimePickerFragment
Class:
public class TimePickerFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute, DateFormat.is24HourFormat(getActivity()));
}
}
实际上比我想象的要简单,您需要做的就是添加一个 dismiss 侦听器并相应地更改您的 ui/logic
TimePickerDialog dialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
}
}, 10, 10, true);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
Toast.makeText(MainActivity.this, "dismissed", Toast.LENGTH_LONG).show();
}
});
dialog.show();
您可以使用标准的 TimePickerDialog
编辑:将除 dialog.show()
之外的所有内容都放入 onCreate()
,dialog.show()
进入 Switch
中的 ifChecked
正如我在您的代码中看到的,您没有在 TimePickerFragment class 中定义任何侦听器。您可以在 TimePickerFragment 中添加以下代码:
public class TimePickerFragment(Context context) extends DialogFragment{
interface OnCompleteListener {
abstract void onComplete(boolean suceeded);
}
OnCompleteListener listener;
/**
* your other lines of code
**/
}
不要忘记为您的 class 设置上下文参数!
然后重写 onAttach 函数并在其中实现你的监听器,如下所示:
try {
this.listener = (OnCompleteListener) context;
} catch (ClassCastException e) {
throw ClassCastException(activity.toString() + " must implement OnCompleteListener");
}
在此之后,在您的重写 onDismiss() 中并在其中设置 listener.onComplete(false);
。每当 TimePickerFragment 想要成功完成时,只需设置 listener.onComplete(true);
现在我们将回到 MainActivity。首先让MainActivity实现TimePickerFragment.OnCompleteListener
。
现在您需要覆盖 OnCompleteListener
的抽象函数。简单地说,如果参数为假,则意味着对话框未成功关闭,因此使用简单的 if 条件以编程方式将 Switch
设置为关闭。
希望能帮到你。
我已经尝试实现一个由 SwitchCompat 打开的时间选择器,所以当我启用该选项时,时间选择器会出现,它工作正常但如果我按下取消或后退按钮或简单地按下在时间选择器之外,开关保持在打开位置,当我不在时间选择器中选择时间时,我希望它回到关闭状态。以为 onCancel 方法可能有帮助,但没有,或者只是我在实现它时犯了一些错误,有相关代码:
public class Settings extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener, TimePickerDialog.OnDismissListener, TimePickerDialog.OnCancelListener {
private EditText textLastHour;
private KeyListener keyListener;
private SwitchCompat hourSwitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
textLastHour = findViewById(R.id.finalHourText);
textLastHour.setKeyListener(null);
textLastHour.setHint("Option not selected");
hourSwitch = findViewById(R.id.switch_hour);
hourSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
DialogFragment timePicker = new TimePickerFragment();
timePicker.show(getSupportFragmentManager(), "time picker");
} else {
textLastHour.setText("");
}
}
});
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
textLastHour = findViewById(R.id.finalHourText);
textLastHour.setText("Final alarm at: " + hourOfDay + ":" + minute);
}
@Override
public void onCancel(DialogInterface dialog) {
hourSwitch.setChecked(false);
}
@Override
public void onDismiss(DialogInterface dialog) {
hourSwitch.setChecked(false);
}
}
onDismiss
和onCancel
我都试过了,这里都不行,请问取消Time Picker后,有没有办法让Switch回到原来的位置?将 DatePicker
添加到标签中,因为我觉得它可能与 TimePicker
对于该问题具有相同的解决方案。
我的TimePickerFragment
Class:
public class TimePickerFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute, DateFormat.is24HourFormat(getActivity()));
}
}
实际上比我想象的要简单,您需要做的就是添加一个 dismiss 侦听器并相应地更改您的 ui/logic
TimePickerDialog dialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
}
}, 10, 10, true);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
Toast.makeText(MainActivity.this, "dismissed", Toast.LENGTH_LONG).show();
}
});
dialog.show();
您可以使用标准的 TimePickerDialog
编辑:将除 dialog.show()
之外的所有内容都放入 onCreate()
,dialog.show()
进入 Switch
正如我在您的代码中看到的,您没有在 TimePickerFragment class 中定义任何侦听器。您可以在 TimePickerFragment 中添加以下代码:
public class TimePickerFragment(Context context) extends DialogFragment{
interface OnCompleteListener {
abstract void onComplete(boolean suceeded);
}
OnCompleteListener listener;
/**
* your other lines of code
**/
}
不要忘记为您的 class 设置上下文参数!
然后重写 onAttach 函数并在其中实现你的监听器,如下所示:
try {
this.listener = (OnCompleteListener) context;
} catch (ClassCastException e) {
throw ClassCastException(activity.toString() + " must implement OnCompleteListener");
}
在此之后,在您的重写 onDismiss() 中并在其中设置 listener.onComplete(false);
。每当 TimePickerFragment 想要成功完成时,只需设置 listener.onComplete(true);
现在我们将回到 MainActivity。首先让MainActivity实现TimePickerFragment.OnCompleteListener
。
现在您需要覆盖 OnCompleteListener
的抽象函数。简单地说,如果参数为假,则意味着对话框未成功关闭,因此使用简单的 if 条件以编程方式将 Switch
设置为关闭。
希望能帮到你。