我已经按照Google的建议,将 TextInputEditText 包裹在 TextInputLayout 周围:https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html

但是,我的TextInputEditText上设置了一个OnClickListener,因此当用户点击它时,可以弹出DatePickerDialog。

我的问题是,实际上,在第一次点击时,除了提示标签移至TextInputEditText上方之外,什么都没有发生。
第二次点击时,显示DatePickerDialog。

我如何才能使DatePickerDialog第一次出现?
在TextInutLayout而不是TextInputEditText上设置onClickListener无效。
有人有主意吗?

最佳答案

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

关于android - 环绕TextInputLayout的TextInputEditText中的OnClickListener,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40670296/

10-11 22:59