android虚拟键盘的监控,显示和隐藏
听到一个键盘监控【显示、隐藏】的问题,上网找了下解决方案。
有提到使用onSizeChange方法监控的,也有监控onLayout方法的。
思路:在弹出键盘时,系统会调整布局大小,通过监控这个布局高度的变换,确认键盘是否显示和隐藏。
现在自己动手实践下,测试记录下demo。
直接上代码:
======================
重载view的方法,并提供接口 ========================
package com.pig.view;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
public class KeyboardLinearLayout extends LinearLayout {
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 OnKeyBoardChangeListeneronKeyBoardChangeListener;
private boolean mHasInit;
private boolean mHasKeybord;
private int mHeight;
public interface OnKeyBoardChangeListener{
public void onKeyBoardStateChange(int state);
}
public KeyboardLinearLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public KeyboardLinearLayout(Context context) {
super(context);
}
@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(null != onKeyBoardChangeListener){
onKeyBoardChangeListener.onKeyBoardStateChange(KEYBOARD_STATE_INIT);
}
}else{
mHeight = mHeight < b ? b : mHeight;
}
if(mHasInit && mHeight
> b){
mHasKeybord = true;
if(null != onKeyBoardChangeListener){
onKeyBoardChangeListener.onKeyBoardStateChange(KEYBOARD_STATE_SHOW);
}
System.out.println("show keyboard...");
}
if(mHasInit && mHasKeybord
&& mHeight == b){
mHasKeybord = false;
if(null != onKeyBoardChangeListener){
onKeyBoardChangeListener.onKeyBoardStateChange(KEYBOARD_STATE_HIDE);
}
System.out.println("hide keyboard...");
}
}
public void setOnKeyBoardChangeListener(OnKeyBoardChangeListeneronKeyBoardChangeListener) {
this.onKeyBoardChangeListener= onKeyBoardChangeListener;
}
}
==================
使用view的布局 =================
encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/information"/>
android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="5dp"
android:paddingBottom="5dp">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/inspection_date"/>
android:id="@+id/inspection_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
================ 程序使用方式 ===============
package com.pig.inspection;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.widget.Toast;
import com.pig.view.KeyboardLinearLayout;
import
com.pig.view.KeyboardLinearLayout.OnKeyBoardChangeListener;
public class Information extends Activity {
private KeyboardLinearLayout informationLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
informationLayout = (KeyboardLinearLayout)
LayoutInflater.from(this).inflate(R.layout.information, null);
informationLayout.setOnKeyBoardChangeListener(onKeyBoardChangeListener);
setContentView(informationLayout);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount() ==
0){
new AlertDialog.Builder(Information.this)
.setTitle(R.string.quit)
.setMessage(R.string.are_you_sure)
.setPositiveButton(R.string.yes, new
DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
System.exit(0);
}
}).setNegativeButton(R.string.no, new
DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
}).show();
}
return true;
//return super.onKeyDown(keyCode, event);
}
public OnKeyBoardChangeListeneronKeyBoardChangeListener= new
OnKeyBoardChangeListener(){
@Override
public void onKeyBoardStateChange(int state) {
// TODO Auto-generated method stub
switch(state){
case KeyboardLinearLayout.KEYBOARD_STATE_INIT:
Toast.makeText(Information.this, "KEYBOARD_STATE_INIT",
Toast.LENGTH_SHORT).show();
break;
case KeyboardLinearLayout.KEYBOARD_STATE_HIDE:
Toast.makeText(Information.this, "KEYBOARD_STATE_HIDE",
Toast.LENGTH_SHORT).show();
break;
case KeyboardLinearLayout.KEYBOARD_STATE_SHOW:
Toast.makeText(Information.this, "KEYBOARD_STATE_SHOW",
Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
};
}