转载地址:http://www.eoeandroid.com/thread-157446-1-1.html
今天看到社区里面有人问关于如何检测软键盘的弹起和隐藏事件。正确处理好软键盘的弹起和隐藏可以大大提升应用的体验。这一点,“切客优惠”做的很好。
在软键盘弹起后,下面的分享内容自动隐藏,并且在titlebar上,显示签到按钮。这个是一个非常贴心的设计,用户体检大大提升。
这是被大家视为无解的难题,我给大家提供一种方法。
有图为证:
初始化页面
刀客优化版初始页
优化版键盘弹起
软键盘隐藏
嘿嘿,未优化版的钉子户
钉子户果然坚挺啊
下面谈谈偶的实现原理:
使用自定义布局,页面布局中包含ScrollVIew,在软键盘弹起后,布局的高度会发生改变,根据布局的高度来判断软键盘的状态。
[mw_shl_code=java,true]package com.ransj.keyboard;
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);
}
}[/mw_shl_code]
这个是自定义的布局,自定义布局可以继承各种常见布局。自定义布局有键盘状态改变监听器,可以通过注册监听器来监听软键盘状态。
[mw_shl_code=java,true]<?xml version="1.0" encoding="utf-8"?>
<com.ransj.keyboard.KeyboardLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboardLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<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="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/testone_tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="center"
android:text="软件盘弹起,我将消失!软键盘隐藏,我将回来!"
android:layout_weight="1.0"
android:textColor="#00ff00"
android:textStyle="bold" />
<EditText
android:id="@+id/testone_et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></EditText>
</LinearLayout>
</ScrollView>
</com.ransj.keyboard.KeyboardLayout>[/mw_shl_code]
这个是优化页面的布局,从上面可以看出自定义布局的用法。
[mw_shl_code=java,true]package com.ransj.keyboard;
import com.ransj.keyboard.KeyboardLayout.onKybdsChangeListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
/**
*
*
*/
public class TestOne extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testone);
KeyboardLayout mainView = (KeyboardLayout) findViewById(R.id.keyboardLayout1);
final TextView tv = (TextView) findViewById(R.id.testone_tv);
mainView.setOnkbdStateListener(new onKybdsChangeListener() {
public void onKeyBoardStateChange(int state) {
switch (state) {
case KeyboardLayout.KEYBOARD_STATE_HIDE:
tv.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "软键盘隐藏", Toast.LENGTH_SHORT).show();
break;
case KeyboardLayout.KEYBOARD_STATE_SHOW:
tv.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(), "软键盘弹起", Toast.LENGTH_SHORT).show();
break;
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onPause() {
super.onPause();
}
}[/mw_shl_code]
这个是优化页面的代码逻辑,通过注册监听器,来监听软键盘事件。在软键盘弹起时隐藏Textview,在软键盘隐藏时显示Textview。
附上源码:
KeyBoard.zip
欢迎大家反馈交流。