TextInputEditText中的OnClickListener包裹在TextInputLayout中

问题描述 投票:8回答:2

我有一个围绕TextInputLayout的TextInputEditText,正如Google建议的那样:https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html

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

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

android android-layout android-edittext android-textinputlayout datepickerdialog
2个回答
10
投票

仅在第二次单击时打开日历是因为您使用的是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
        }
    }
});

0
投票

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

android:focusableInTouchMode="false"

在您的edittext xml代码中。有关更多信息,请访问here

© www.soinside.com 2019 - 2024. All rights reserved.