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

使用自定义布局,页面布局中包含ScrollVIew,在软键盘弹起后,布局的高度会发生改变,根据布局的高度来判断软键盘的状态。
转自:[url]http://www.eoeandroid.com/thread-157446-1-1.html[/url]
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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值