单击关闭时如何删除突出显示的EditText

时间:2016-05-04 09:11:10

标签: android android-edittext

我有很多EditText,当我点击一个按钮关闭时,我取消所有的焦点,但我得到的结果是它突出显示第一个EditText,我无法带走这个荧光笔。我想重置所有初始条件EditExt

 private void buttonClick(){

    mTxtFld2.setCursorVisible(false);
    mTxtFld3.setCursorVisible(false);
    mTxtFld4.setCursorVisible(false);
    mTxtFld5.setCursorVisible(false);

    mTxtFld2.clearFocus();
    mTxtFld3.clearFocus();
    mTxtFld4.clearFocus();
    mTxtFld5.clearFocus();

    mTxtFld2.setSelectAllOnFocus(false);
    mTxtFld3.setSelectAllOnFocus(false);
    mTxtFld4.setSelectAllOnFocus(false);
    mTxtFld5.setSelectAllOnFocus(false);

    mTxtFld2.setBackgroundColor(Color.TRANSPARENT);
    mTxtFld3.setBackgroundColor(Color.TRANSPARENT);
    mTxtFld4.setBackgroundColor(Color.TRANSPARENT);
    mTxtFld5.setBackgroundColor(Color.TRANSPARENT);

    View v = getActivity().getCurrentFocus();
    if (v == null) return;

    InputMethodManager imm = (InputMethodManager)
            getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}

在xml中

 <EditText
            android:layout_width="621dp"
            android:layout_height="44dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="21dp"
            android:id="@+id/EditText2"
            android:inputType="textNoSuggestions"
            android:fontFamily="fonts/Montserrat-Bold.ttf"
            android:selectAllOnFocus="true"
            android:gravity="center"
            android:singleLine="true"
            android:focusable="true"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:hint="@string/name"
            android:textColorHint="@color/text_placeholder"
            android:background="@drawable/back" />

1 个答案:

答案 0 :(得分:1)

您可以使用隐藏的可聚焦布局来设置焦点:

<LinearLayout
    android:id="@+id/dummy_focus"
    android:layout_width="0px"
    android:layout_height="0px"
    android:focusable="true"
    android:focusableInTouchMode="true" />

在offButton操作中,将焦点设置在此布局上。