检测软键盘的弹起与隐藏【绝对经典,好用】

使用自定义布局,页面布局中包含ScrollVIew,在软键盘弹起后,布局的高度会发生改变,根据布局的高度来判断软键盘的状态。
转自: http://www.eoeandroid.com/thread-157446-1-1.html
1. 新建一个布局类KeyboardLayout
/**   
 * @文件名: KeyboardLayout.java 
 * @包 com.nhii.widget 
 * @描述: TODO(用一句话描述该文件做什么) 
 * @作者:MaLijun   
 * @创建时间 2013-12-15 上午10:16:37 
 * @版本 V1.0  
 */
package com.nhii.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RelativeLayout;

public class KeyboardLayout extends RelativeLayout {
	private static final String TAG = KeyboardLayout.class.getSimpleName();
	public static final byte KEYBOARD_STATE_SHOW = -3;
	public static final byte KEYBOARD_STATE_HIDE = -2;
	public static final byte KEYBOARD_STATE_INIT = -1;
	private boolean mHasInit;
	private boolean mHasKeybord;
	private int mHeight;
	private onKybdsChangeListener mListener;

	public KeyboardLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public KeyboardLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public KeyboardLayout(Context context) {
		super(context);
	}

	/** * set keyboard state listener */
	public void setOnkbdStateListener(onKybdsChangeListener listener) {
		mListener = listener;
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		if (!mHasInit) {
			mHasInit = true;
			mHeight = b;
			if (mListener != null) {
				mListener.onKeyBoardStateChange(KEYBOARD_STATE_INIT);
			}
		} else {
			mHeight = mHeight < b ? b : mHeight;
		}
		if (mHasInit && mHeight > b) {
			mHasKeybord = true;
			if (mListener != null) {
				mListener.onKeyBoardStateChange(KEYBOARD_STATE_SHOW);
			}
			Log.w(TAG, "show keyboard.......");
		}
		if (mHasInit && mHasKeybord && mHeight == b) {
			mHasKeybord = false;
			if (mListener != null) {
				mListener.onKeyBoardStateChange(KEYBOARD_STATE_HIDE);
			}
			Log.w(TAG, "hide keyboard.......");
		}
	}

	public interface onKybdsChangeListener {
		public void onKeyBoardStateChange(int state);
	}
}


2. activity中
package com.nhii.ui;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Window;
import android.widget.Toast;
import com.nhii.R;
import com.nhii.widget.KeyboardLayout;
import com.nhii.widget.KeyboardLayout.onKybdsChangeListener;
import com.ta.annotation.TAInjectView;

public class TestPageActivity extends BaseActivity {

	@TAInjectView(id = R.id.keyboardLayout1)
	KeyboardLayout mainView;
	
	@Override
	protected void onPreOnCreate(Bundle savedInstanceState) {
		super.onPreOnCreate(savedInstanceState);
		// 取消标题
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		// 竖屏锁定
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}

	@Override
	protected void onAfterSetContentView() {
		super.onAfterSetContentView();
		mainView.setOnkbdStateListener(new onKybdsChangeListener() {
			public void onKeyBoardStateChange(int state) {
				switch (state) {
				case KeyboardLayout.KEYBOARD_STATE_HIDE:
					Toast.makeText(getApplicationContext(), "软键盘隐藏", Toast.LENGTH_SHORT).show();
					break;                                 
				case KeyboardLayout.KEYBOARD_STATE_SHOW:
					Toast.makeText(getApplicationContext(), "软键盘弹起", Toast.LENGTH_SHORT).show();
					break;
				}
			}
		}); 
	}
}


3. xml页面
<com.nhii.widget.KeyboardLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboardLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/light_pink"
    android:orientation="vertical" >
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:fillViewport="true" >
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >
            <EditText
                android:id="@+id/testone_et"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="请输入..." />
        </LinearLayout>
    </ScrollView>
</com.nhii.widget.KeyboardLayout>

你可能感兴趣的:(软键盘)