我希望能够从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);
其他回答
我也有同样的问题。这简直让我疯了。
我有一个扩展的对话框与一个ScrollView,它有一个表布局与扩展的线性布局,其中包含一个搜索栏和一个编辑文本。
第一个EditText在显示对话框后总是自动对焦,在完成键盘上的文本编辑后,EditText仍然有焦点,键盘仍然可见。
我几乎尝试了这个线程的所有解决方案,没有一个对我有效。
所以这里我的简单解决方案:(text = EditText)
text.setOnEditorActionListener( new OnEditorActionListener( ){
public boolean onEditorAction( TextView v, int actionId, KeyEvent event ){
if( (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) ||
(actionId == EditorInfo.IME_ACTION_DONE) ){
text.clearFocus( );
InputMethodManager iMgr = null;
iMgr = (InputMethodManager)mContext.getSystemService( Context.INPUT_METHOD_SERVICE );
iMgr.hideSoftInputFromWindow( text.getWindowToken(), 0 );
}
return true;
}
});
顺便说一下,我没有使用以下任何片段来解决它:
//setFocusableInTouchMode( true )
//setFocusable( true )
//setDescendantFocusability( ViewGroup.FOCUS_BEFORE_DESCENDANTS )
而且我没有使用像宽高为1dp的视图这样的间隔项。
希望它能帮助到某人:D
在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" />
试着在你的视图上使用这个 这招对我很管用:
<View
android:id="@+id/fucused"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"/>
你可以通过设置父元素的属性android:descendantFocusability来避免对元素的任何关注。
这里有一个例子:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/search__scroller"
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ScrollView>
在这里,属性android:descendantFocusability设置为“blocksDescendants”是阻止对子元素的关注。
你可以在这里找到更多信息。
你也可以包括android:windowSoftInputMode="stateAlwaysHidden"在你的manifest动作部分。
这相当于:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
推荐文章
- Android Studio, logcat在应用程序关闭后清理
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的
- 在Mac OS X上哪里安装Android SDK ?
- 我如何获得图像缩放功能?
- 在Android应用程序中显示当前时间和日期
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)
- 如何在新的材质主题中改变背面箭头的颜色?