如何在单击EditText时查​​找DatePicker片段?

时间:2015-09-07 22:07:17

标签: android android-fragments datepicker android-edittext

我有一个小问题需要修复DatePicker片段。用户单击UI屏幕上的EditText行,并在第一次单击时立即启动DatePcker片段。用户选择日期,代码在EditText行上设置日期。使用闪烁的光标将焦点返回到EditText行。

现在,如果用户导航到另一个EditText行,然后返回到DatePicker EditText行以选择其他日期,则需要两次单击才能再次启动Picker。第一次单击获得焦点,第二次单击启动Picker,我尝试在第一次单击时启动Picker。注意" onFocusChange"的空虚测试。是确保在设置日期后发生方向更改时片段对话框不会启动。

部分活动文件:

...
fListenerEditText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DialogFragment newFragment = new DatePickerFragment();
            newFragment.show(getSupportFragmentManager(), "datePicker");
            // The below line gets the Floating Label color to switch back to the primary Hint color.
            // Also allows the back button code to work correctly, to go back to the previous activity and clear
            // the soft keyboard with one press, not two.
            fListenerEditText.requestFocus();
        }
    });

    fListenerEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // If fListernerEditText gains focus then launch DatePicker
            // else nothing is done is fListenerEditText loses focus.
            // Also the length() test is a test for emptiness;  that is, only display
            // the DatePicker dialog if the EditText is empty.  That way, if there is already a
            // a date set in the EditText line and an orientation change occurs, the new Activity
            // and Fragment won't re-launch the DatePicker dialog again because the length() test won't show empty.
            if (hasFocus && (fListenerEditText.getText().length() == 0)) {
                DialogFragment newFragment = new DatePickerFragment();
                newFragment.show(getSupportFragmentManager(), "datePicker");
                // The below line gets the Floating Label color to switch back to the primary Hint color.
                // Also allows the back button code to work correctly, to go back to the previous activity and clear
                // the soft keyboard with one press, not two.
                fListenerEditText.requestFocus();
            }
        }
    });

0 个答案:

没有答案