我希望能够取消EditText的焦点。比如说,如果键盘弹出并且用户通过返回按钮隐藏它,我希望焦点和光标都消失。如何实现这个功能?
我希望能够取消EditText的焦点。比如说,如果键盘弹出并且用户通过返回按钮隐藏它,我希望焦点和光标都消失。如何实现这个功能?
你可以通过以下方法使光标和焦点消失:
edittext.clearFocus();
但是检测键盘隐藏是一项艰难的工作。
android:focusable="true"
和android:focusableInTouchMode="true"
,这将把焦点从“EditText”更改为“Layout”。 - Loyalar您可以将此代码添加到 onCreate
中,这样每次Activity
启动时就会隐藏键盘。
您还可以以编程方式将焦点更改为另一个项。
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
和 editText.requestFocus();
。 - Prabs在你的XML中,在EditText
之前添加LinearLayout
。
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
android:layout_width="0px"
android:layout_height="0px" />
或者您可以通过在“EditText”之前将以下行添加到视图中来执行相同的操作。
<Button
android:id="@+id/btnSearch"
android:layout_width="50dp"
android:layout_height="50dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:text="Quick Search"
android:textColor="#fff"
android:textSize="13sp"
android:textStyle="bold" />
<EditText
android:id="@+id/edtSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:gravity="left"
android:hint="Name"
android:maxLines="1"
android:singleLine="true"
android:textColorHint="@color/blue"
android:textSize="13sp"
android:textStyle="bold" />
android:focusableInTouchMode="true"
,因为将其设置为 true 也会确保该视图可聚焦。 - GuilhEeditText.setFocusableInTouchMode(false);
editText.setFocusable(false);
editText.setFocusableInTouchMode(true);
editText.setFocusable(true);
EditText将失去焦点,但可以在新的触摸事件上再次获得焦点。
将这两个属性添加到您的父布局中(例如:线性布局、相对布局)
android:focusable="true"
android:focusableInTouchMode="true"
它能解决问题 :)
这对我有效。
编辑 在链接中他们建议使用 LinearLayout,但是简单的 View 也可以工作。
<View
android:id="@+id/focus_thief"
android:layout_width="1dp"
android:layout_height="1dp"
android:focusable="true"
android:focusableInTouchMode="true" />
如果将这个"theif"放在布局的顶部(成为第一个可聚焦的项目),则调用clearFocus()
函数将起作用。
在活动开始时隐藏键盘,请在onCreate()中编写以下代码。
InputMethodManager imm = (InputMethodManager)
getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
清除焦点并从编辑框中移除光标......
editText.clearFocus();
editText.setCursorVisible(false);
editText.clearFocus();
正常工作,您需要在根布局中添加android:focusableInTouchMode="true" android:focusable="true"
。 - DIRTY DAVE你也可以在清单文件的操作部分中包含android:windowSoftInputMode="stateAlwaysHidden"。
这相当于:
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);
尝试在您的视图中使用此代码,它对我起作用:
<View
android:id="@+id/fucused"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"/>
将以下代码添加到您的父布局中,确保您已经将 EditText
放置在正确位置,并添加 android:focusableInTouchMode="true"
属性:
clearFocus()
清除了它的焦点,但然后开始在视图树中搜索焦点候选者,最终发现相同的视图。结果是一样的。但如果你添加android:focusableInTouchMode
,焦点遍历会停留在其父视图上。 - WindRider