我希望能够从EditText删除焦点。例如,如果键盘出现,用户用后退按钮隐藏它,我希望焦点和光标消失。怎样才能做到呢?
当前回答
试着在你的视图上使用这个 这招对我很管用:
<View
android:id="@+id/fucused"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"/>
其他回答
删除自动聚焦编辑文本android
这对我很有用
在链接中,他们建议使用线性布局,但简单的视图也可以
<View
android:id="@+id/focus_thief"
android:layout_width="1dp"
android:layout_height="1dp"
android:focusable="true"
android:focusableInTouchMode="true" />
然后,如果这个“小偷”被放置在布局的顶部(第一个可聚焦的项目),调用clearFocus()将工作。
你也可以包括android:windowSoftInputMode="stateAlwaysHidden"在你的manifest动作部分。
这相当于:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
在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: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);
你可以把这个添加到onCreate,它将隐藏键盘每次活动启动。
您还可以通过编程方式将焦点更改为另一个项目。
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件