前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android禁止EditText自动弹出软键盘的方法及遇到问题

Android禁止EditText自动弹出软键盘的方法及遇到问题

作者头像
砸漏
发布2020-11-05 09:51:07
3.1K0
发布2020-11-05 09:51:07
举报
文章被收录于专栏:恩蓝脚本

平时开发中经常遇到的很小的问题,这里记录一下。

一般在AndroidManifest.xml中添加了android:windowSoftInputMode="adjustResize"或者adjustPan的话,页面中包含EditText控件进入时会自动弹出软件盘。

1.在包含EditText的父布局中添加android:focusable="true"和android:focusableInTouchMode="true"

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"? 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:focusable="true"
   android:focusableInTouchMode="true"
   
  <EditText
    android:id="@+id/edit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:maxLines="1"
    / 
</LinearLayout 

这样可以禁止自动弹出软键盘

2.在AndroidManifest.xml中添加stateHidden,这样也不会自动弹出

代码语言:javascript
复制
<activity android:name=".TestAActivity"
   android:windowSoftInputMode="adjustResize|stateHidden" 
</activity 

3.进入页面强制隐藏软键盘

如果前两种方法都不起作用的话,可以使用这种方法:

代码语言:javascript
复制
/**
 * 隐藏输入软键盘
 * @param context
 * @param view
 */
 public static void hideInputManager(Context context,View view){
   InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
   if (view !=null && imm != null){
     imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏
   }
 }

总结

以上所述是小编给大家介绍的Android禁止EditText自动弹出软键盘的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对ZaLou.Cn网站的支持!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档