android监控虚拟键盘,android虚拟键盘的监控,显示和隐藏

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;

}

}

};

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值