我希望能够从EditText删除焦点。例如,如果键盘出现,用户用后退按钮隐藏它,我希望焦点和光标消失。怎样才能做到呢?


当前回答

editText.setFocusableInTouchMode(true)

EditText将能够在用户触摸它时获得焦点。 当主布局(活动,对话框等)变得可见时,EditText不会自动获得焦点,即使它是布局中的第一个视图。

其他回答

你必须删除<requestFocus/>

如果你不使用它,还是同样的问题

user LinearLayout作为父类并设置

android:focusable="true"
android:focusableInTouchMode="true"

希望对你有所帮助。

移除焦点但保持可聚焦:

editText.setFocusableInTouchMode(false);
editText.setFocusable(false);
editText.setFocusableInTouchMode(true);
editText.setFocusable(true);

EditText将失去焦点,但可以在新的触摸事件中再次获得焦点。

你也可以包括android:windowSoftInputMode="stateAlwaysHidden"在你的manifest动作部分。

这相当于:

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

而是以XML的方式。

供参考,你也可以用代码隐藏键盘:

// hide virtual keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mYourEditText.getWindowToken(), 0);

您可以使光标和焦点消失

edittext.clearFocus();

但检测时隐藏键盘是一项艰苦的工作。

check your xml file
 <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp" >

            **<requestFocus />**
 </EditText>


//Remove  **<requestFocus />** from xml