TextInputLayout onclick中的EditText需要2单击?!安卓 [英] EditText inside TextInputLayout onclick requires 2 click ?! Android
问题描述
我只是想在TextInputLayout内部的Edit文本上进行一次onlick侦听.它可以工作,但是我需要单击两次EditText才能触发它,我不明白为什么.这是我的代码:
I am simply trying to have an onlick listen on an Edit text inside a TextInputLayout. It works but I need to click on the EditText twice for it to trigger I dont understand why. Here is my code:
xml:
<android.support.design.widget.TextInputLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<EditText
android:id="@+id/start_date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Starting Date*: "
android:inputType="textPersonName" />
</android.support.design.widget.TextInputLayout>
听众:
private void setListenners() {
EditText startDate = (EditText) mView.findViewById(R.id.start_date);
startDate.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v) {
Calendar mcurrentDate=Calendar.getInstance();
int mYear = mcurrentDate.get(Calendar.YEAR);
int mMonth = mcurrentDate.get(Calendar.MONTH);
int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);
DatePickerDialog mDatePicker=new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
Log.d("DEBUG", "year: " + year + " month: " + month + " day: " + day);
}
},mYear, mMonth, mDay);
mDatePicker.show();
}
});
}
推荐答案
将属性 android:focusableInTouchMode
设置为 false
android:focusableInTouchMode="false"
在您的edittext
xml代码中.
in your edittext
xml code.
根据文档的说明, android:focusableInTouchMode
是:
Explanation, as from docs, android:focusableInTouchMode
is:
布尔值,控制视图在触摸时是否可以聚焦 模式.如果对于某个视图而言这是正确的,则该视图可以在以下情况下获得焦点 单击,如果单击另一个视图,则可以保持焦点 没有将此属性设置为true.
Boolean that controls whether a view can take focus while in touch mode. If this is true for a view, that view can gain focus when clicked on, and can keep focus if another view is clicked on that doesn't have this attribute set to true.
,EditText
默认为 true .
换句话说:第一次单击将使edittext
获得焦点,而第二次单击将触发ClickListener
.因此,您应该禁用对触摸的关注.
In other words: the first click will make the edittext
to gain focus and second click is the one that triggers the ClickListener
. So you should disable gaining focus on touch.
这篇关于TextInputLayout onclick中的EditText需要2单击?!安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!