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


当前回答

当活动开始时隐藏键盘。在onCreate()中编写以下代码..

InputMethodManager imm = (InputMethodManager)
getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);

清除焦点并从edittext.....中删除光标

editText.clearFocus();

editText.setCursorVisible(false);

其他回答

editText.setFocusableInTouchMode(true)

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

添加到你的父布局,你把你的EditText这个android:focusableInTouchMode="true"

将这两个属性添加到你的父布局(例如:线性布局,相对布局)

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

它会成功的:)

移除焦点但保持可聚焦:

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

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

你必须删除<requestFocus/>

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

user LinearLayout作为父类并设置

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

希望对你有所帮助。