Android 如何从EditText正确触发日期选择器和验证?
我试图对editText字段进行一些验证,请允许我详细说明,因为我使用了一堆在stackOverflow中都有自己问题的概念,但我感觉我试图做的事情在其中任何一个中都没有得到回答,这是一个相当基本的问题,所以要么我搞砸了,要么我只是发现了一个bug,而且,许多流行的问题和答案都是几年前的 我有两个Edittext字段,其中包含从日期选择器输入的初始日期和终止日期,我正在验证在日期选择器输入终止日期后,该日期是否发生在初始日期之后,如果没有,则使用setError显示错误 到目前为止,一切都很好,但有件事困扰着我: 当我第一次单击编辑文本时,它什么也不做(因为我使用Android 如何从EditText正确触发日期选择器和验证?,android,datepicker,android-edittext,user-experience,Android,Datepicker,Android Edittext,User Experience,我试图对editText字段进行一些验证,请允许我详细说明,因为我使用了一堆在stackOverflow中都有自己问题的概念,但我感觉我试图做的事情在其中任何一个中都没有得到回答,这是一个相当基本的问题,所以要么我搞砸了,要么我只是发现了一个bug,而且,许多流行的问题和答案都是几年前的 我有两个Edittext字段,其中包含从日期选择器输入的初始日期和终止日期,我正在验证在日期选择器输入终止日期后,该日期是否发生在初始日期之后,如果没有,则使用setError显示错误 到目前为止,一切都很好,
setInputType(InputType.TYPE\u NULL)
以及android:focusableInTouchMode=“true”
在XML中;为了避免显示键盘,因此它会单击、被选中,并且没有键盘,但也没有日期选择器,直到再次单击,onClick方法触发日期选择器,所有操作都按预期进行。)
我本来打算给你看一些图片,但我没有代表点:/
如果我使用setFocusable(false)
,当setError出现时,会发生一些奇怪的事情,只有图标显示,没有文本
如果我不使用setFocusable(false)
,则当活动开始时,编辑文本显示为选中状态(由于我禁用了输入,因此没有光标闪烁),并保持选中状态(它保持蓝色,但您可以单击任意位置)
如果我不想在开始时选择它,我会根据这个问题()的建议在父布局的XML中使用android:focusableInTouchMode=“true
”,这就解决了这个问题。但是我有第一次点击,第二次点击什么也没有发生,再次显示日期选择器问题。。。我做错了什么
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#FFFFFF"
android:layout_height="match_parent"
android:gravity="center_vertical"
style="@style/AppTheme"
android:id="@+id/fechasalariolayout"
android:focusableInTouchMode="true">
关于错误消息和聚焦性,我也有类似的“问题”。如果文本视图不可聚焦,则不会显示消息。我想这就是它的工作原理,例如,如果您有许多字段经过验证,并且每个字段中都有验证错误,则只会为包含光标的textView显示消息
在一个应用程序中,我使用时间和日期选择器的方式与您相同。我有已验证但不可聚焦的文本视图。我的布局如下所示:
<LinearLayout
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/box_bg"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="13dp"
android:text="@string/start_time" />
<EditText
android:id="@+id/editText_start_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:focusable="false"
android:inputType="none"
android:onClick="showTimePicker" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="13dp"
android:text="@string/end_time" />
<EditText
android:id="@+id/editText_end_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:focusable="false"
android:inputType="none"
android:onClick="showTimePicker" />
</LinearLayout>
也许这可以解决文本视图自动获取焦点的问题。如果没有,请告诉我们,我们会找到解决办法
[编辑]
下面是我正在使用的另一个代码段:
<TableRow
android:id="@+id/tablerow"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:background="@drawable/box_bg"
android:baselineAligned="false">
<TextView
android:id="@+id/text_view_birthday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="center_vertical"
android:layout_span="1"
android:text="@string/birthday" />
<EditText
android:id="@+id/text_birthday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_gravity="center_vertical"
android:cursorVisible="false"
android:editable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:inputType="date|textNoSuggestions"
android:layout_weight="1"
android:layout_span="2" />
</TableRow>
您指的是文本视图还是编辑文本?如果我使用android:focusable=“false”,正如您所描述的,我最终会遇到“setError with icon but not text”问题,就像我使用setFocusable(false)一样,这与我认为的完全相同,对不起,我是指editText,但我认为,这也应该适用于文本视图。是的,这是我们应该期待的结果。您无法聚焦editText/textView,这就是您无法查看消息的原因。你所能看到的只是一个图标。我想这是不可聚焦视图的正确行为。我不确定这是正常行为,因为它也不会显示图标,但如果是这样,那么,它如何保持聚焦,同时避免在您再次单击之前不触发日期选择器的问题?我也有同样的问题,但不幸的是,我不确定如何解决它:(.如果你第一次点击视图,它会显示光标吗?如果不会,那么我想其他视图会获得焦点。也许你可以尝试实现一个自定义或一个自定义。它不会显示光标,因为setInputType(InputType.TYPE\u NULL),但如果不是这样的话,你可能是对的,重点在别处。让我更深入地了解一下。谢谢:)
<LinearLayout
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/box_bg"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="13dp"
android:text="@string/start_time" />
<EditText
android:id="@+id/editText_start_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:focusable="false"
android:inputType="none"
android:onClick="showTimePicker" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="13dp"
android:text="@string/end_time" />
<EditText
android:id="@+id/editText_end_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:focusable="false"
android:inputType="none"
android:onClick="showTimePicker" />
</LinearLayout>
<TableRow
android:id="@+id/tablerow"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:background="@drawable/box_bg"
android:baselineAligned="false">
<TextView
android:id="@+id/text_view_birthday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="center_vertical"
android:layout_span="1"
android:text="@string/birthday" />
<EditText
android:id="@+id/text_birthday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_gravity="center_vertical"
android:cursorVisible="false"
android:editable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:inputType="date|textNoSuggestions"
android:layout_weight="1"
android:layout_span="2" />
</TableRow>