Android禁止EditText自动弹出软键盘的方法

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

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

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

<?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>

123456789101112131415161718

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

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

<activity android:name=".TestAActivity"

    android:windowSoftInputMode="adjustResize|stateHidden">

</activity>123

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

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

/**

  * 隐藏输入软键盘

  * @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);  //强制隐藏

    }

}

————————————————

版权声明:本文为CSDN博主「川峰」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/lyabc123456/article/details/81076031

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容