TextInputEditText中的OnClickListener包裹在TextInputLayout中

And*_*ife 9 android android-layout android-edittext datepickerdialog android-textinputlayout

我有一个TextInputEditText周围包裹TextInputLayout,如谷歌建议做:https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html

但是,我的TextInputEditText上设置了一个OnClickListener,因此当用户点击它时,可以弹出一个DatePickerDialog.
我的问题是,事实上,在第一次点击时除了提示标签移动到TextInputEditText之外没有任何反应.第二次点击时,会显示DatePickerDialog.

我怎么能在第一次点击时显示DatePickerDialog?
在TextInutLayout而不是TextInputEditText上设置onClickListener不起作用.有人有想法吗?

Har*_*iya 11

仅在第二次单击时打开日历是因为您正在使用edittext.在第一次单击时,您的编辑文本将获得焦点.然后第二次单击只调用onClickListener.

如果您不期望手动编辑日期(使用键盘),那么为什么不使用TextView显示所选日期?

首次点击时显示日历:

如果您需要使用EditText并在第一次单击时加载日历,则尝试设置onFocusChangeListner为editText而不是onClickListner.

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus) {
           // Show your calender here 
        } else {
           // Hide your calender here
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的回复伙伴,我将在明天恢复工作时实施它,并在有效时标记您的回复。回答您的问题:**为什么不使用 TextView 来显示选定的日期?** 嗯,您知道,来自 UX/设计团队的那些猴子... (2认同)

小智 8

将属性 android:focusableInTouchMode 设置为 false,如下所示:

android:focusableInTouchMode="false"
Run Code Online (Sandbox Code Playgroud)

在您的 edittext xml 代码中。欲了解更多信息,请访问此处