单击按钮时清除 EditText 的焦点 - Android
Clear focus from EditText on button click - Android
- 共有 5 个 EditText。
- 我在 EditText 的 setOnFocusChangeListener 中调用一个方法,即 txt_location.
- 当我单击 保存并继续 按钮时,我正在验证所有 EditText。
- 当我单击 保存并继续 按钮时 txt_location - EditText 自动获得我不想要的焦点。
我尝试过的:
- txt_location.clearFocus()
- other_edittext.requestFocus()
我的代码如下。
AddAddressFragment.java
以下方法在 onActivityCreated 方法中。
txt_location.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
txt_location.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
findPlace();
}
});
findPlace();
}
}
});
layoutFooter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isvalidateFields()){
// Code after validating all EditTexts
}
}
});
以下方法在 onActivityCreated 方法之外。
private boolean isvalidateFields() {
boolean result = true;
if (!ValidationUtils.hasText(txtCity, cityTxt, "City Required")) {
result = false;
}
if (!ValidationUtils.hasText(txt_location, location_txt, "Location Required")) {
result = false;
}
if (!ValidationUtils.hasText(txtPincode, pincodeTxt, "Pincode Required"))
result = false;
if (!ValidationUtils.hasText(txtDoorName, doorNumberTxt, "Address line 1 Required")) {
result = false;
}
if (!ValidationUtils.hasText(txtApartmentName, apartmentTxt, "Address line 2 Required")) {
result = false;
}
return result;
}
address_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:fillViewport="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="15px">
<LinearLayout
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/titleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="@dimen/large_margin"
android:layout_marginBottom="@dimen/normal_margin"
android:text="Address"
android:textSize="@dimen/text_content" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="@dimen/small_margin"
android:button="@drawable/check_box"
android:checked="true"
android:clickable="false" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="@dimen/small_margin"
android:background="@color/gray_border" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/large_margin"
android:orientation="vertical"
android:padding="@dimen/large_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:id="@+id/doorNumberTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:theme="@style/TextLabel">
<EditText
android:id="@+id/txtDoorName"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="@dimen/normal_margin"
android:layout_marginTop="@dimen/normal_margin"
android:background="@android:color/transparent"
android:hint="@string/door_number"
android:singleLine="true"
android:textColorHighlight="@color/gray_text"
android:textColorHint="@color/gray_text"
android:textSize="@dimen/text_medium" />
</android.support.design.widget.TextInputLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="@dimen/normal_margin"
android:layout_marginTop="@dimen/normal_margin"
android:background="@color/gray_border" />
<android.support.design.widget.TextInputLayout
android:id="@+id/apartmentTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:theme="@style/TextLabel">
<EditText
android:id="@+id/txtApartmentName"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="@dimen/normal_margin"
android:layout_marginTop="@dimen/normal_margin"
android:background="@android:color/transparent"
android:hint="@string/apartment_number"
android:singleLine="true"
android:textColorHighlight="@color/gray_text"
android:textColorHint="@color/gray_text"
android:textSize="@dimen/text_medium" />
</android.support.design.widget.TextInputLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="@dimen/normal_margin"
android:layout_marginTop="@dimen/normal_margin"
android:background="@color/gray_border" />
<android.support.design.widget.TextInputLayout
android:id="@+id/location_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:theme="@style/TextLabel">
<EditText
android:id="@+id/txt_location"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="@dimen/normal_margin"
android:layout_marginTop="@dimen/normal_margin"
android:background="@android:color/transparent"
android:clickable="false"
android:hint="@string/txt_location"
android:lines="2"
android:textColorHighlight="@color/gray_text"
android:textColorHint="@color/gray_text"
android:textSize="@dimen/text_medium" />
</android.support.design.widget.TextInputLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="@dimen/normal_margin"
android:layout_marginTop="@dimen/normal_margin"
android:background="@color/gray_border" />
<android.support.design.widget.TextInputLayout
android:id="@+id/cityTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:theme="@style/TextLabel">
<EditText
android:id="@+id/txtCity"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="@dimen/normal_margin"
android:layout_marginTop="@dimen/normal_margin"
android:background="@android:color/transparent"
android:clickable="false"
android:hint="@string/city"
android:lines="5"
android:textColorHighlight="@color/gray_text"
android:textColorHint="@color/gray_text"
android:textSize="@dimen/text_medium" />
</android.support.design.widget.TextInputLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="@dimen/normal_margin"
android:layout_marginTop="@dimen/normal_margin"
android:background="@color/gray_border" />
<android.support.design.widget.TextInputLayout
android:id="@+id/pincodeTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:theme="@style/TextLabel">
<EditText
android:id="@+id/txtPincode"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="@dimen/normal_margin"
android:layout_marginTop="@dimen/normal_margin"
android:background="@android:color/transparent"
android:digits="1234567890"
android:hint="@string/pincode"
android:inputType="textPostalAddress"
android:singleLine="true"
android:textColorHighlight="@color/gray_text"
android:textColorHint="@color/gray_text"
android:textSize="@dimen/text_medium" />
</android.support.design.widget.TextInputLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="@dimen/normal_margin"
android:layout_marginTop="@dimen/normal_margin"
android:background="@color/gray_border" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:id="@+id/layoutFooter"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:layout_alignParentBottom="true"
android:background="@color/fab_button_color">
<TextView
android:id="@+id/txtLblInr"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="@dimen/x_large_margin"
android:gravity="center_vertical"
android:singleLine="true"
android:text="@string/save_n_contine"
android:textAllCaps="true"
android:textColor="@color/white"
android:textSize="@dimen/text_title" />
<TextView
android:id="@+id/txtTotalContinue"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginRight="@dimen/x_large_margin"
android:drawableRight="@drawable/right_arrow"
android:gravity="center"
android:singleLine="true"
android:textColor="@color/white"
android:textSize="@dimen/text_title" />
</RelativeLayout>
</LinearLayout>
为您的父布局提供 ID,例如:LinearLayout,然后 linearLayout.requestFocus()
然后把这个
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:descendantFocusability="beforeDescendants" <-- add this
android:focusableInTouchMode="true" <-- add this
>
.....
</LinearLayout>
我找到了替代解决方案,因为 ZeroOne 的 答案对我不起作用。我删除了 setOnFocusChangeListener 并将 setOnEditorActionListener 放在 txt_location - EditText 之前的 EditText 上.我把 setOnTouchListener 放到 txt_location - EditText.
setOnEditorActionListener - 当您点击键盘上的 NEXT LINE 按钮时调用。
完整修改后的代码如下。
EditTextJustBefore_txt_location_EditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
findPlace();
}
return true;
}
});
txt_location.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
findPlace();
}
return false;
}
});
- 共有 5 个 EditText。
- 我在 EditText 的 setOnFocusChangeListener 中调用一个方法,即 txt_location.
- 当我单击 保存并继续 按钮时,我正在验证所有 EditText。
- 当我单击 保存并继续 按钮时 txt_location - EditText 自动获得我不想要的焦点。
我尝试过的:
- txt_location.clearFocus()
- other_edittext.requestFocus()
我的代码如下。
AddAddressFragment.java
以下方法在 onActivityCreated 方法中。
txt_location.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
txt_location.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
findPlace();
}
});
findPlace();
}
}
});
layoutFooter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isvalidateFields()){
// Code after validating all EditTexts
}
}
});
以下方法在 onActivityCreated 方法之外。
private boolean isvalidateFields() {
boolean result = true;
if (!ValidationUtils.hasText(txtCity, cityTxt, "City Required")) {
result = false;
}
if (!ValidationUtils.hasText(txt_location, location_txt, "Location Required")) {
result = false;
}
if (!ValidationUtils.hasText(txtPincode, pincodeTxt, "Pincode Required"))
result = false;
if (!ValidationUtils.hasText(txtDoorName, doorNumberTxt, "Address line 1 Required")) {
result = false;
}
if (!ValidationUtils.hasText(txtApartmentName, apartmentTxt, "Address line 2 Required")) {
result = false;
}
return result;
}
address_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:fillViewport="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="15px">
<LinearLayout
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/titleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="@dimen/large_margin"
android:layout_marginBottom="@dimen/normal_margin"
android:text="Address"
android:textSize="@dimen/text_content" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="@dimen/small_margin"
android:button="@drawable/check_box"
android:checked="true"
android:clickable="false" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="@dimen/small_margin"
android:background="@color/gray_border" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/large_margin"
android:orientation="vertical"
android:padding="@dimen/large_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:id="@+id/doorNumberTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:theme="@style/TextLabel">
<EditText
android:id="@+id/txtDoorName"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="@dimen/normal_margin"
android:layout_marginTop="@dimen/normal_margin"
android:background="@android:color/transparent"
android:hint="@string/door_number"
android:singleLine="true"
android:textColorHighlight="@color/gray_text"
android:textColorHint="@color/gray_text"
android:textSize="@dimen/text_medium" />
</android.support.design.widget.TextInputLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="@dimen/normal_margin"
android:layout_marginTop="@dimen/normal_margin"
android:background="@color/gray_border" />
<android.support.design.widget.TextInputLayout
android:id="@+id/apartmentTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:theme="@style/TextLabel">
<EditText
android:id="@+id/txtApartmentName"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="@dimen/normal_margin"
android:layout_marginTop="@dimen/normal_margin"
android:background="@android:color/transparent"
android:hint="@string/apartment_number"
android:singleLine="true"
android:textColorHighlight="@color/gray_text"
android:textColorHint="@color/gray_text"
android:textSize="@dimen/text_medium" />
</android.support.design.widget.TextInputLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="@dimen/normal_margin"
android:layout_marginTop="@dimen/normal_margin"
android:background="@color/gray_border" />
<android.support.design.widget.TextInputLayout
android:id="@+id/location_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:theme="@style/TextLabel">
<EditText
android:id="@+id/txt_location"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="@dimen/normal_margin"
android:layout_marginTop="@dimen/normal_margin"
android:background="@android:color/transparent"
android:clickable="false"
android:hint="@string/txt_location"
android:lines="2"
android:textColorHighlight="@color/gray_text"
android:textColorHint="@color/gray_text"
android:textSize="@dimen/text_medium" />
</android.support.design.widget.TextInputLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="@dimen/normal_margin"
android:layout_marginTop="@dimen/normal_margin"
android:background="@color/gray_border" />
<android.support.design.widget.TextInputLayout
android:id="@+id/cityTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:theme="@style/TextLabel">
<EditText
android:id="@+id/txtCity"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="@dimen/normal_margin"
android:layout_marginTop="@dimen/normal_margin"
android:background="@android:color/transparent"
android:clickable="false"
android:hint="@string/city"
android:lines="5"
android:textColorHighlight="@color/gray_text"
android:textColorHint="@color/gray_text"
android:textSize="@dimen/text_medium" />
</android.support.design.widget.TextInputLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="@dimen/normal_margin"
android:layout_marginTop="@dimen/normal_margin"
android:background="@color/gray_border" />
<android.support.design.widget.TextInputLayout
android:id="@+id/pincodeTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:theme="@style/TextLabel">
<EditText
android:id="@+id/txtPincode"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="@dimen/normal_margin"
android:layout_marginTop="@dimen/normal_margin"
android:background="@android:color/transparent"
android:digits="1234567890"
android:hint="@string/pincode"
android:inputType="textPostalAddress"
android:singleLine="true"
android:textColorHighlight="@color/gray_text"
android:textColorHint="@color/gray_text"
android:textSize="@dimen/text_medium" />
</android.support.design.widget.TextInputLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="@dimen/normal_margin"
android:layout_marginTop="@dimen/normal_margin"
android:background="@color/gray_border" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:id="@+id/layoutFooter"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:layout_alignParentBottom="true"
android:background="@color/fab_button_color">
<TextView
android:id="@+id/txtLblInr"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="@dimen/x_large_margin"
android:gravity="center_vertical"
android:singleLine="true"
android:text="@string/save_n_contine"
android:textAllCaps="true"
android:textColor="@color/white"
android:textSize="@dimen/text_title" />
<TextView
android:id="@+id/txtTotalContinue"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginRight="@dimen/x_large_margin"
android:drawableRight="@drawable/right_arrow"
android:gravity="center"
android:singleLine="true"
android:textColor="@color/white"
android:textSize="@dimen/text_title" />
</RelativeLayout>
</LinearLayout>
为您的父布局提供 ID,例如:LinearLayout,然后 linearLayout.requestFocus()
然后把这个
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:descendantFocusability="beforeDescendants" <-- add this
android:focusableInTouchMode="true" <-- add this
>
.....
</LinearLayout>
我找到了替代解决方案,因为 ZeroOne 的 答案对我不起作用。我删除了 setOnFocusChangeListener 并将 setOnEditorActionListener 放在 txt_location - EditText 之前的 EditText 上.我把 setOnTouchListener 放到 txt_location - EditText.
setOnEditorActionListener - 当您点击键盘上的 NEXT LINE 按钮时调用。
完整修改后的代码如下。
EditTextJustBefore_txt_location_EditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
findPlace();
}
return true;
}
});
txt_location.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
findPlace();
}
return false;
}
});