哦,天啊,我两天前也遇到了这个问题。我找不到同时让它有焦点(以便显示消息)和创建日期选择器弹出窗口的方法。最终我做的是像这样将EditText
包装在一个TextInputLayout
中:
<android.support.design.widget.TextInputLayout
android:id="@+id/birthDateInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/input_birth_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/birth_date_hint"
android:inputType="date" />
</android.support.design.widget.TextInputLayout>
然后,我不是在编辑文本上设置错误,而是在TextInputLayout
上设置错误,像这样:
birthDateInputLayout.setErrorEnabled(true);
birthDateInputLayout.setError("This field cannot be empty.");
注意:这种方法和常规的在
EditText
上设置错误的方式不完全相同,但它看起来足够好,并以不同的方式解决了问题。
if(((EditText)view).getError() == null) {
//Handle your click for showing picker
}
试试这个,
onClick(View v){
if(editText.getError() != null ){
editText.requestFocus(); // to show error message
}else{
// open date picker dialog here or do you stuff here
}
希望能有所帮助
EditText
是可聚焦的情况下才行。不幸的是,在我的情况下,我不希望用户手动输入日期。我想到的一个解决方案是使用TextView
而不是EditText
。 - Xc0d3r您可以根据您的代码使用以下代码,当所有字段都填写完毕并且第二个日期编辑文本设置为null时,将错误设置为null。如有任何疑问,请随时提问。
mEditText.setError(null);//removes error
mEditText.clearFocus(); //clear focus from edit text