彻底搞定Android开发中软键盘的常见问题

软键盘显示的原理

   软件盘的本质是什么?软键盘其实是一个Dialog。 
   InputMethodService为我们的输入法创建了一个Dialog,并且将该Dialog的Window的某些参数(如Gravity)进行了设置,使之能够在底部或者全屏显示。当我们点击输入框时,系统对活动主窗口进行调整,从而为输入法腾出相应的空间,然后将该Dialog显示在底部,或者全屏显示。

软键盘显示的调整

    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-默认模式:该模式下通过不会调整来保证软键盘的空间,而是采取了另外一种策略,系统会通过布局的移动,来保证用户要进行输入的输入框肯定在用户的视野范围里面,从而让用户可以看到自己输入的内容。

没有滚动布局xml

<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:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="文本输入框1" />

    <Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="猴子搬来的救兵按钮"
android:textSize="15sp" />


    <EditText
android:id="@+id/et2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="文本输入框2" />

    <EditText
android:id="@+id/et3"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="文本输入框3" />

    <EditText
android:id="@+id/et4"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="文本输入框4" />

    <EditText
android:id="@+id/et5"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="文本输入框5" />

    <EditText
android:id="@+id/et6"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="文本输入框6" />

    <EditText
android:id="@+id/et7"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="文本输入框7" />

    <EditText
android:id="@+id/et8"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="文本输入框8" />

    <EditText
android:id="@+id/et9"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="文本输入框9" />

    <EditText
android:id="@+id/et10"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="文本输入框10" />

    <EditText
android:id="@+id/et11"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="文本输入框11" />

    <EditText
android:id="@+id/et12"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="文本输入框12" />
</LinearLayout>
对于没有滚动控件的布局来说,adjustPan就是默认的设置,比如我们案例应用中的文本输入8,上面的文本输入框123和按钮都会被顶上去,且页面布局不可以滚动。

有滚动布局xml

<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:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="文本输入框1" />

        <Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="猴子搬来的救兵按钮"
android:textSize="15sp" />


        <EditText
android:id="@+id/et2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="文本输入框2" />

        <EditText
android:id="@+id/et3"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="文本输入框3" />

        <EditText
android:id="@+id/et4"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="文本输入框4" />

        <EditText
android:id="@+id/et5"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="文本输入框5" />

        <EditText
android:id="@+id/et6"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="文本输入框6" />

        <EditText
android:id="@+id/et7"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="文本输入框7" />

        <EditText
android:id="@+id/et8"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="文本输入框8" />

        <EditText
android:id="@+id/et9"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="文本输入框9" />

        <EditText
android:id="@+id/et10"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="文本输入框10" />

        <EditText
android:id="@+id/et11"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="文本输入框11" />

        <EditText
android:id="@+id/et12"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="文本输入框12" />
    </LinearLayout>
   </ScrollView>

对于有滚动控件的布局,则是采用的adjustResize方式,比如我们案例应用中的文本输入8,上面的文本输入框123和按钮都会被顶上去,可以通过滚动来查看被顶上去的内容。
根据这一原理,我们就可以把开发中遇到的软键盘遮挡页面的问题,利用ScrollView当做根布局,让系统采用adjustResize模式,很好地解决这一问题。

参考文章链接:http://blog.csdn.net/mynameishuangshuai/article/details/51567357

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值