按钮点击时从EditText清除焦点 - Android

时间:2017-05-16 15:21:44

标签: android android-edittext android-widget android-textinputedittext onfocuschangelistener

  • 共有5个EditTexts。
  • 我在EditText的 setOnFocusChangeListener 中调用一个方法,即 txt_location
  • 当我点击保存并继续按钮时,我会验证所有EditTexts。
  • 当我点击保存并继续按钮 txt_location - EditText 时,会自动获得我不想要的焦点。

我尝试过:

  • txt_location.clearFocus()
  • other_edittext.requestFocus()

我的代码如下。

AddAddressFragment.java

以下方法采用 onActivityCreated 方法。

*

下面的方法不在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
            }
        }
    });

address_fragment.xml

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;
}

2 个答案:

答案 0 :(得分:0)

将Id提供给您的父布局ex: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> 

答案 1 :(得分:0)

我找到了替代解决方案,因为 ZeroOne&#39> 答案对我不起作用。我删除 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;
        }
    });