Android 判断软键盘的状态(显示,隐藏)


先上截图,有图有真相:



自定义RelativeLayout

  1. package com.demo.softkeyboard;  
  2.   
  3. import Android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.widget.RelativeLayout;  
  6.   
  7. public class KeyboardListenRelativeLayout extends RelativeLayout {  
  8.       
  9.     private static final String TAG = KeyboardListenRelativeLayout.class.getSimpleName();  
  10.       
  11.     public static final byte KEYBOARD_STATE_SHOW = -3;  
  12.     public static final byte KEYBOARD_STATE_HIDE = -2;  
  13.     public static final byte KEYBOARD_STATE_INIT = -1;  
  14.       
  15.     private boolean mHasInit = false;  
  16.     private boolean mHasKeyboard = false;  
  17.     private int mHeight;  
  18.       
  19.     private IOnKeyboardStateChangedListener onKeyboardStateChangedListener;  
  20.       
  21.     public KeyboardListenRelativeLayout(Context context) {  
  22.         super(context);  
  23.     }  
  24.     public KeyboardListenRelativeLayout(Context context, AttributeSet attrs) {  
  25.         super(context, attrs);  
  26.     }  
  27.       
  28.     public KeyboardListenRelativeLayout(Context context, AttributeSet attrs, int defStyle) {  
  29.         super(context, attrs, defStyle);  
  30.     }  
  31.       
  32.     public void setOnKeyboardStateChangedListener(IOnKeyboardStateChangedListener onKeyboardStateChangedListener) {  
  33.         this.onKeyboardStateChangedListener = onKeyboardStateChangedListener;  
  34.     }  
  35.       
  36.     @Override  
  37.     protected void onLayout(boolean changed, int l, int t, int r, int b) {  
  38.         super.onLayout(changed, l, t, r, b);  
  39.         if(!mHasInit) {  
  40.             mHasInit = true;  
  41.             mHeight = b;  
  42.             if(onKeyboardStateChangedListener != null) {  
  43.                 onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_INIT);  
  44.             }  
  45.         } else {  
  46.             mHeight = mHeight < b ? b : mHeight;  
  47.         }  
  48.           
  49.         if(mHasInit && mHeight > b) {  
  50.             mHasKeyboard = true;  
  51.             if(onKeyboardStateChangedListener != null) {  
  52.                 onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_SHOW);  
  53.             }  
  54.         }  
  55.         if(mHasInit && mHasKeyboard && mHeight == b) {  
  56.             mHasKeyboard = false;  
  57.             if(onKeyboardStateChangedListener != null) {  
  58.                 onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_HIDE);  
  59.             }  
  60.         }  
  61.     }  
  62.       
  63.     public interface IOnKeyboardStateChangedListener {  
  64.         public void onKeyboardStateChanged(int state);  
  65.     }  
  66. }  
用法:
  1. package com.demo.softkeyboard;  
  2.   
  3. import com.demo.softkeyboard.KeyboardListenRelativeLayout.IOnKeyboardStateChangedListener;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.EditText;  
  9. import android.widget.TextView;  
  10.   
  11. /** 
  12.  * 软键盘监听Demo 
  13.  * @author qiaoning 
  14.  * 
  15.  */  
  16. public class SoftKeyboardListenDemoActivity extends Activity {  
  17.       
  18.     private EditText editText;  
  19.     KeyboardListenRelativeLayout relativeLayout;  
  20.       
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.main);  
  25.           
  26.         relativeLayout = (KeyboardListenRelativeLayout) findViewById(R.id.keyboardRelativeLayout);  
  27.         editText = (EditText) findViewById(R.id.editText);  
  28.           
  29.         relativeLayout.setOnKeyboardStateChangedListener(new IOnKeyboardStateChangedListener() {  
  30.               
  31.             public void onKeyboardStateChanged(int state) {  
  32.                 switch (state) {  
  33.                 case KeyboardListenRelativeLayout.KEYBOARD_STATE_HIDE://软键盘隐藏   
  34.                     editText.setVisibility(View.VISIBLE);  
  35.                     break;  
  36.                 case KeyboardListenRelativeLayout.KEYBOARD_STATE_SHOW://软键盘显示   
  37.                     editText.setVisibility(View.GONE);  
  38.                     break;  
  39.                 default:  
  40.                     break;  
  41.                 }  
  42.             }  
  43.         });  
  44.     }  
  45. }  
使用到的布局文件:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <com.demo.softkeyboard.KeyboardListenRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/keyboardRelativeLayout"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.     <ScrollView android:layout_width="fill_parent"  
  7.         android:layout_height="fill_parent"  
  8.         android:layout_alignParentLeft="true"  
  9.         android:layout_alignParentTop="true"  
  10.         android:fillViewport="true">  
  11.         <LinearLayout android:layout_width="fill_parent"  
  12.             android:layout_height="wrap_content"  
  13.             android:orientation="vertical">  
  14.             <EditText android:id="@+id/editText"  
  15.                 android:layout_width="fill_parent"  
  16.                 android:layout_height="wrap_content"/>  
  17.         </LinearLayout>  
  18.     </ScrollView>  
  19. </com.demo.softkeyboard.KeyboardListenRelativeLayout>  

源码下载地址

免费下载地址在 http://linux.bkjia.com/

用户名与密码都是www.bkjia.com

具体下载目录在 /2012年资料/6月/30日/Android 判断软键盘的状态(显示,隐藏)

相关内容