简体   繁体   English

DialogFragment timepicker onCancel 和 onDismiss 问题

[英]DialogFragment timepicker onCancel and onDismiss problem

I've tried to implement a Time Picker which is opened up by a SwitchCompat, so when I enable the option, the Time Picker shows up, it works fine but if I press cancel, or back button or simply press outside of the time picker, the Switch stays at the ON position and I want it to get back to OFF when I don't choose the time in Time Picker.我试图实现一个由 SwitchCompat 打开的时间选择器,所以当我启用该选项时,时间选择器会出现,它工作正常,但如果我按下取消、后退按钮或只是在时间选择器之外按下,开关保持在 ON 位置,当我没有在 Time Picker 中选择时间时,我希望它回到 OFF 状态。 Thought that maybe onCancel method could help but it does not, or simply I made some mistake with implementing it, there's the related code:认为 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);
    }
}

I've tried both onDismiss and onCancel and neither have worked here, is there any solution to make the Switch go back to the original position if you cancel the Time Picker?我已经尝试过onDismissonCancel ,但都没有在这里工作,如果取消时间选择器,是否有任何解决方案可以使 Switch 回到原始位置? Added the DatePicker to tags as I feel like it may have the same solution as TimePicker for that problem.DatePicker添加到标签,因为我觉得它可能具有与TimePicker相同的解决方案。

My TimePickerFragment Class:我的TimePickerFragment类:

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()));
    }
}

It's actually simpler than I thought all you need to do is to add a dismiss listener and change your ui/logic accordingly它实际上比我想象的要简单,您需要做的就是添加一个解除监听器并相应地更改您的用户界面/逻辑

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();

You can use the standard TimePickerDialog您可以使用标准的 TimePickerDialog

Edit: Put everything to onCreate() except for dialog.show() , dialog.show() goes into the ifChecked in the Switch编辑:将除dialog.show() onCreate()之外的所有内容都放到onCreate()dialog.show()进入 Switch 中的 ifChecked

As I see in your code you haven't defined any listener in your TimePickerFragment class.正如我在您的代码中看到的,您尚未在 TimePickerFragment 类中定义任何侦听器。 you can add following code in your TimePickerFragment :您可以在 TimePickerFragment 中添加以下代码:

public  class TimePickerFragment(Context context) extends DialogFragment{

    interface OnCompleteListener {
        abstract void onComplete(boolean suceeded);
    }

    OnCompleteListener listener;

    /**

    * your other lines of code

    **/
}

Don't forget to set context argument for your class !不要忘记为您的班级设置上下文参数!

Then override onAttach function and inside that implement your listener like this :然后覆盖 onAttach 函数并在里面实现你的监听器,如下所示:

try {
            this.listener = (OnCompleteListener) context;

        } catch (ClassCastException e) {

            throw ClassCastException(activity.toString() + " must implement OnCompleteListener");

        }

After this in your override onDismiss() and set listener.onComplete(false);在此之后在您的覆盖 onDismiss() 中设置listener.onComplete(false); inside that.在那里面。 And whenever the TimePickerFragment wanted to finish successfully just set listener.onComplete(true);每当 TimePickerFragment 想要成功完成时,只需设置listener.onComplete(true);

Now we shall back to MainActivity.现在我们将回到 MainActivity。 First of all make MainActivity implement TimePickerFragment.OnCompleteListener .首先让 MainActivity 实现TimePickerFragment.OnCompleteListener
Now you need to override the abstract function of your OnCompleteListener .现在您需要覆盖OnCompleteListener的抽象函数。 Simply if the argument was false it means that dialog dismissed unsuccessfully, so with a simple if condition set your Switch to off programmatically.简单地说,如果参数为假,则意味着对话框未成功关闭,因此使用简单的 if 条件以编程方式将您的Switch设置为关闭。

Hope I could help.希望我能帮上忙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM