android 软键盘弹出时的界面控制

来源:互联网 发布:淘宝上卖铁观音 编辑:程序博客网 时间:2024/06/05 06:20

一、软键盘显示的原理


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

 

 

二、活动主窗口调整


    android定义了一个属性,名字为windowSoftInputMode, 用它可以让程序可以控制活动主窗口调整的方式。我们可以在AndroidManifet.xml中对Activity进行设置。如:android:windowSoftInputMode="stateUnchanged|adjustPan"

 


模式一,压缩模式

 

    windowSoftInputMode的值如果设置为adjustResize,那么该Activity主窗口总是被调整大小以便留出软键盘的空间。
我们通过一段代码来测试一下,当我们设置了该属性后,弹出输入法时,系统做了什么。


    重写Layout布局:

Java代码  
  1. public class ResizeLayout extends LinearLayout{    
  2.     private static int count = 0;    
  3.         
  4.     public ResizeLayout(Context context, AttributeSet attrs) {    
  5.         super(context, attrs);    
  6.     }    
  7.         
  8.     @Override    
  9.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {        
  10.         super.onSizeChanged(w, h, oldw, oldh);    
  11.             
  12.         Log.e("onSizeChanged " + count++, "=>onResize called! w="+w    
  13.                 + ",h="+h+",oldw="+oldw+",oldh="+oldh);    
  14.     }    
  15.         
  16.     @Override    
  17.     protected void onLayout(boolean changed, int l, int t, int r, int b) {    
  18.         super.onLayout(changed, l, t, r, b);    
  19.         Log.e("onLayout " + count++, "=>OnLayout called! l=" + l + ", t="    
  20.                     + t + ",r=" + r + ",b="+b);    
  21.     }    
  22.         
  23.     @Override    
  24.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    
  25.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);    
  26.             
  27.         Log.e("onMeasure " + count++, "=>onMeasure called! widthMeasureSpec="    
  28.                 + widthMeasureSpec + ", heightMeasureSpec=" + heightMeasureSpec);    
  29.     }    
  30. }  
public class ResizeLayout extends LinearLayout{ private static int count = 0; public ResizeLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); Log.e("onSizeChanged " + count++, "=>onResize called! w="+w + ",h="+h+",oldw="+oldw+",oldh="+oldh); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); Log.e("onLayout " + count++, "=>OnLayout called! l=" + l + ", t=" + t + ",r=" + r + ",b="+b); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); Log.e("onMeasure " + count++, "=>onMeasure called! widthMeasureSpec=" + widthMeasureSpec + ", heightMeasureSpec=" + heightMeasureSpec); } }

 

我们的布局设置为:

 

Xml代码  
  1. <com.winuxxan.inputMethodTest.ResizeLayout     
  2.     xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:id="@+id/root_layout"    
  4.     android:layout_width="fill_parent"    
  5.     android:layout_height="fill_parent"    
  6.     android:orientation="vertical"    
  7.     >    
  8.         
  9.     <EditText    
  10.         android:layout_width="fill_parent"     
  11.         android:layout_height="wrap_content"     
  12.     />    
  13.       
  14.     <LinearLayout    
  15.             android:id="@+id/bottom_layout"    
  16.             android:layout_width="fill_parent"     
  17.             android:layout_height="fill_parent"     
  18.             android:orientation="vertical"    
  19.             android:gravity="bottom">s    
  20.        
  21.     <TextView      
  22.         android:layout_width="fill_parent"     
  23.         android:layout_height="wrap_content"     
  24.         android:text="@string/hello"    
  25.         android:background="#77777777"    
  26.       />    
  27.    </LinearLayout>    
  28. </com.winuxxan.inputMethodTest.ResizeLayout>   
<com.winuxxan.inputMethodTest.ResizeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root_layout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" /> <LinearLayout android:id="@+id/bottom_layout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="bottom">s <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:background="#77777777" /> </LinearLayout> </com.winuxxan.inputMethodTest.ResizeLayout>

 

AndroidManifest.xml的Activity设置属性:android:windowSoftInputMode = "adjustResize"
    运行程序,点击文本框,查看调试信息:
    E/onMeasure 6(7960): =>onMeasure called! widthMeasureSpec=1073742144, heightMeasureSpec = 1073742024
    E/onMeasure 7(7960): =>onMeasure called! widthMeasureSpec=1073742144, heightMeasureSpec = 1073742025
    E/onSizeChanged 8(7960): =>onSizeChanged called! w=320,h=201,oldw=320,oldh=377
    E/onLayout 9(7960): =>OnLayout called! l=0, t=0,r=320,b=201
    从调试结果我们可以看出,当我们点击文本框后,根布局调用了onMeasure,onSizeChanged和onLayout。
    实际上,当设置为adjustResize后,软键盘弹出时,要对主窗口布局重新进行measure和layout,而在layout时,发现窗口的大小发生的变化,因此调用了onSizeChanged。
从下图的运行结果我们也可以看出,原本在下方的TextView被顶到了输入法的上方

 

 

 模式二,平移模式


    windowSoftInputMode的值如果设置为adjustPan,那么该Activity主窗口并不调整屏幕的大小以便留出软键盘的空间。相反,当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分。这个通常是不期望比调整大小,因为用户可能关闭软键盘以便获得与被覆盖内容的交互操作。
    上面的例子中,我们将AndroidManifest.xml的属性进行更改:android: windowSoftInputMode = "adjustPan"

    重新运行,并点击文本框,查看调试信息:
    E/onMeasure 6(8378): =>onMeasure called! widthMeasureSpec=1073742144, heightMeasureSpec=1073742200
    E/onMeasure 7(8378): =>onMeasure called! widthMeasureSpec=1073742144, heightMeasureSpec=1073742201
    E/onLayout 8(8378): =>OnLayout called! l=0, t=0,r=320,b=377
    我们看到:系统也重新进行了measrue和layout,但是我们发现,layout过程中onSizeChanged并没有调用,这说明输入法弹出前后并没有改变原有布局的大小。
    从下图的运行结果我们可以看到,下方的TextView并没有被顶到输入法上方。

 事实上,当输入框不会被遮挡时,该模式没有对布局进行调整,然而当输入框将要被遮挡时,窗口就会进行平移。也就是说,该模式始终是保持输入框为可见。如下图,整个窗口,包括标题栏均被上移,以保证文本框可见。

 

 

 模式三 自动模式


    当属性windowSoftInputMode被设置为adjustUspecified时,它不被指定是否该Activity主窗口调整大小以便留出软键盘的空间,或是否窗口上的内容得到屏幕上当前的焦点是可见的。系统将自动选择这些模式中一种主要依赖于是否窗口的内容有任何布局视图能够滚动他们的内容。如果有这样的一个视图,这个窗口将调整大小,这样的假设可以使滚动窗口的内容在一个较小的区域中可见的。这个是主窗口默认的行为设置。
    也就是说,系统自动决定是采用平移模式还是压缩模式,决定因素在于内容是否可以滚动。