Android软键盘的隐藏显示

Android软键盘的隐藏显示对输入框和布局的影响。


1. 平移模式:android:windowSoftInputMode="adjustPan"
layout 文件:
[java]  view plain copy
  1. <span style="color:#66cccc;"><com.hualu.softinput.RelativeLayoutResize xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_alignParentTop="true"  
  11.         android:layout_marginTop="25dp"  
  12.         android:text="@string/hello_world" />  
  13.   
  14.     <EditText  
  15.         android:id="@+id/editText1"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_alignParentBottom="true"  
  19.         android:layout_centerHorizontal="true"  
  20.         android:ems="10" >  
  21.   
  22.     </EditText>  
  23.   
  24. </com.hualu.softinput.RelativeLayoutResize>  
  25. </span>  

com.hualu.softinput.RelativeLayoutResize:

[java]  view plain copy
  1. package com.hualu.softinput;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.widget.RelativeLayout;  
  6.   
  7. public class RelativeLayoutResize extends RelativeLayout {  
  8.   
  9.     private OnRelativeLayoutResizeListener listener ;  
  10.       
  11.     public RelativeLayoutResize(Context context) {  
  12.         super(context);  
  13.     }  
  14.       
  15.     public RelativeLayoutResize(Context context, AttributeSet attrs) {  
  16.         super(context, attrs) ;  
  17.     }  
  18.       
  19.   
  20.     @Override  
  21.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  22.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  23.         System.out.println("onMeasure() ---> width = " +  widthMeasureSpec + " , height = " + heightMeasureSpec);  
  24.     }  
  25.       
  26.     @Override  
  27.     protected void onLayout(boolean changed, int l, int t, int r, int b) {  
  28.         super.onLayout(changed, l, t, r, b);  
  29.         System.out.println("onLayout() -----> changed = " + changed + " , l = " + l + " , t = " + t + " , r = " + r + " , b = " + b );  
  30.     }  
  31.       
  32.     @Override  
  33.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  34.         super.onSizeChanged(w, h, oldw, oldh);  
  35.         System.out.println("onSizeChanged() ----> w = " + w + " , h = " + h + " , oldW = " + oldw + " , oldH = " + oldh);  
  36.         if(listener != null && h < oldh){  
  37.             listener.onResize() ;  
  38.         }  
  39.     }  
  40.       
  41.     public void setOnRelativeLayoutResizeListener(OnRelativeLayoutResizeListener listener){  
  42.         this.listener = listener ;  
  43.     }  
  44.       
  45.     public interface OnRelativeLayoutResizeListener{  
  46.         void onResize() ;  
  47.     }  
  48.       
  49. }  

启动应用的界面:


打印的Log:


输入法起来:


Log信息:


当界面改变布局时:

[html]  view plain copy
  1. <span style="color:#33cc00;"><com.hualu.softinput.RelativeLayoutResize xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.    
  7.    <EditText  
  8.         android:id="@+id/editText1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_marginLeft="20dp"  
  14.         android:ems="10" >  
  15.    
  16.         <requestFocus />  
  17.    </EditText>  
  18.    
  19.    <TextView  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_alignParentBottom="true"  
  23.         android:layout_alignParentLeft="true"  
  24.         android:layout_marginBottom="64dp"  
  25.         android:text="@string/hello_world" />  
  26.    
  27. </com.hualu.softinput.RelativeLayoutResize></span>  
启动界面为:


Log信息:


启动输入法之后:



Log信息:




结论:

从上面log信息,可以得出在平移模式下,启动输入法和退出输入法都不会出发onSizeChange()方法。



2. 压缩调整模式android:windowSoftInputMode="adjustResize"

当为蓝色Layout时:

启动界面:


Log信息:


退出输入法之后:


Log信息:



当Layout为绿色的内容时:

启动应用界面:


Log信息:


退出输入法之后:


Log信息:



结论:

在压缩模式下, 输入法的启动和退出都会触发onSizeChange()方法的调用。


3. 压缩模式,监听输入法的显示和隐藏:

因此,在压缩模式下可以做到监听输入法的显示与隐藏。

方法如com.hualu.softinput.RelativeLayoutResize中在onSizeChange()方法里面自定义一个事件,监听高度的变法:

[java]  view plain copy
  1. if(listener != null && h < oldh){  
  2.             listener.onResize() ;  
  3.         }  

在Activity中的调用是:

[java]  view plain copy
  1. package com.hualu.softinput;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.widget.TextView;  
  8.   
  9. import com.hualu.softinput.RelativeLayoutResize.OnRelativeLayoutResizeListener;  
  10.   
  11. public class MainActivity extends Activity {  
  12.   
  13.     private TextView text ;  
  14.       
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.         text = (TextView)this.findViewById(R.id.text) ;  
  20.         RelativeLayoutResize rlr = (RelativeLayoutResize)this.findViewById(R.id.contenter) ;  
  21.         rlr.setFocusableInTouchMode(true ) ;  
  22.         rlr.setOnRelativeLayoutResizeListener(new OnRelativeLayoutResizeListener() {  
  23.               
  24.             @Override  
  25.             public void onResize() {  
  26.                 text.setVisibility(View.GONE) ;  
  27.             }  
  28.         }) ;  
  29.     }  
  30.   
  31.     @Override  
  32.     public boolean onCreateOptionsMenu(Menu menu) {  
  33.         // Inflate the menu; this adds items to the action bar if it is present.  
  34.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  35.         return true;  
  36.     }  
  37.   
  38. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值