彻底搞定Android软键盘

首先说明在下使用的手机是Android7.0  华为荣耀6X,如何在下的代码没有做到一些手机或者版本的适配,请大神回复教在下


一.windowSoftInputMode属性

1.概念

(此小节抄录来自https://blog.csdn.net/mynameishuangshuai/article/details/51567357)

        android定义了一个属性,名字为windowSoftInputMode, 这个属性用于设置Activity主窗口与软键盘的交互模式,用于避免软键盘遮挡内容的问题。我们可以在AndroidManifet.xml中对Activity进行设置。如:android:windowSoftInputMode=”stateUnchanged|adjustPan”。 
       该属性可选的值有两部分,一部分为软键盘的状态控制,控制软键盘是隐藏还是显示,另一部分是Activity窗口的调整,以便腾出空间展示软键盘。 
       android:windowSoftInputMode的属性设置必须是下面中的一个值,或一个”state”值加一个”adjust”值的组合,各个值之间用 | 分开。

  • stateUnspecified-未指定状态:当我们没有设置android:windowSoftInputMode属性的时候,软件默认采用的就是这种交互方式,系统会根据界面采取相应的软键盘的显示模式。
  • stateUnchanged-不改变状态:当前界面的软键盘状态,取决于上一个界面的软键盘状态,无论是隐藏还是显示。
  • stateHidden-隐藏状态:当设置该状态时,软键盘总是被隐藏,不管是否有输入的需求。
  • stateAlwaysHidden-总是隐藏状态:当设置该状态时,软键盘总是被隐藏,和stateHidden不同的是,当我们跳转到下个界面,如果下个页面的软键盘是显示的,而我们再次回来的时候,软键盘就会隐藏起来。
  • stateVisible-可见状态:当设置为这个状态时,软键盘总是可见的,即使在界面上没有输入框的情况下也可以强制弹出来出来。
  • stateAlwaysVisible-总是显示状态:当设置为这个状态时,软键盘总是可见的,和stateVisible不同的是,当我们跳转到下个界面,如果下个页面软键盘是隐藏的,而我们再次回来的时候,软键盘就会显示出来。
  • adjustUnspecified-未指定模式:设置软键盘与软件的显示内容之间的显示关系。当你跟我们没有设置这个值的时候,这个选项也是默认的设置模式。在这中情况下,系统会根据界面选择不同的模式。
  • adjustResize-调整模式:该模式下窗口总是调整屏幕的大小用以保证软键盘的显示空间;这个选项不能和adjustPan同时使用,如果这两个属性都没有被设置,系统会根据窗口中的布局自动选择其中一个。
  • adjustPan-默认模式:该模式下通过不会调整来保证软键盘的空间,而是采取了另外一种策略,系统会通过布局的移动,来保证用户要进行输入的输入框肯定在用户的视野范围里面,从而让用户可以看到自己输入的内容。

2.使用

windowSoftInputMode默认的时候stateUnspecified|adjustUnspecified”,当界面可以滚动的时候,弹出软键盘的时候也可以滑动界面,为确保软键盘空间会移动布局

例子如下:

滚动布局

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</ScrollView>

然后我还发现一个奇怪的迹象,如果我把最外层的ScrollView去掉,界面不会默认弹出软键盘

无滚动布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>


windowSoftInputMode属性换成"stateUnspecified|adjustPan",即便界面可以滚动,在弹出软键盘后不能滚动界面,
然后就是以上两种布局不会默认弹出软键盘


windowSoftInputMode属性换成"stateUnspecified|adjustResize",即便界面可以滚动,在弹出软键盘后可以滚动界面,
然后就是以上两种布局默认弹出软键盘

其他state属性和adjust属性之间的配合效果,我就不说了,大家可以试一试


二.控制软键盘弹出

这个是默认为第一个EditText弹出软键盘

        Timer timer=new Timer();
        timer.schedule(new TimerTask() {

            public void run() {
                InputMethodManager inputMethodManager=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }, 1000); // 秒后自动弹出

第二种就可以指定哪个EditText弹出软键盘

        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        MainActivity.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

三.不自动弹出软键盘

先前也说通过通过设置android:windowSoftInputMode="stateUnspecified|adjustPan"  可以设置不自动弹出软键盘,还有一种state属性也可以stateHidden


这些都是在AndroidManifest设置的,还有可以在java代码里可以做到,其实也是设置Window的属性,原理一样

getWindow().setSoftInputMode(   WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);





猜你喜欢

转载自blog.csdn.net/z979451341/article/details/80519748