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;

}

}

};

}

android监控虚拟键盘,android虚拟键盘的监控,显示和隐藏相关推荐

  1. Android炫酷的Toolbar+Bottom+Fab悬浮按钮显示、隐藏、渐变的各种实现姿势

    前言 由于手机屏幕大小的限制,各种控件需要根据需求进行显示,隐藏,移动等,以增加视觉效果,用户体验.就拿目前市场上常见的APP如知乎.QQ.淘宝.美团等来说,在他们的APP里面随处可见一些比较优美的处 ...

  2. [九鼎RK3399Pro] Android 8.1定制系统导航栏和状态栏显示和隐藏可控制

    我们的app跑在RK3399Pro上面,是放在一个公共场合使用,所以把底部的 返回,HOME,MENU键都隐藏掉,并且上部导航栏下拉功能也要取消. 代码我是参考Firefly的代码写的. 实现了一下功 ...

  3. android 360 悬浮窗,360手机卫士悬浮窗显示与隐藏方法

    使用安卓手机的朋友大多安装了360手机卫士吧?安装了360手机卫士安卓版之后就会在桌面上显示一个悬浮窗,那么如何隐藏?又如何开启显示,我们一起来阅读下边的图文教程吧! 需要说明的是:iPhone手机不 ...

  4. android popupwindow边框阴影,android PopupWindow 阴影背景 、动画 、点击显示、隐藏

    阴影背景 bg_shadow_white.xml 入动画 pop_enter_anim.xml 出动画 pop_exit_anim.xml 动画 样式 点击显示.隐藏 final PopupWindo ...

  5. android实现文本输入,Android实现智能提示的文本输入框AutoCompleteTextView

    今天我们要讲一个十分简单的内容,就是一个安卓控件的使用,用法很简单,但是很常用的一个.这里我用两种不同的写法来处理.当然,无论用哪一种写法,效果都是一样的. 我们先来看效果图. 要实现这种效果十分简单 ...

  6. delphi XE模拟Android手机PDA设备的虚拟键盘按键及扫码过程输入焦点及信号接收

    delphi XE模拟Android手机PDA设备的虚拟键盘按键及扫码过程输入焦点及信号接收 今天,群里有几位同学讨论这个问题,汇总了一下,分享出来,供同学们学习研究. 一.直接上代码示例 1.屏蔽应 ...

  7. android自动软键盘,Android自定义软键盘

    MyKeyboard Android自定义键盘的使用 实现步骤 第一步: 1.新建一个xml文件夹放在res目录下面,然后新建xml文件:money_keyboard.xml 2.然后在XML文件中添 ...

  8. android底部滑动出现虚拟按键,Android适配底部虚拟按键的方法详解

    Android适配底部虚拟按键的方法详解 发布时间:2020-10-09 05:26:12 来源:脚本之家 阅读:171 作者:yuanzhihui123 最近项目进行适配的时候发现部分(如华为手机) ...

  9. 安卓平板隐藏虚拟按键_如何隐藏 Android 下方的三个虚拟按键

    满意答案 RaulEP 2016.05.21 采纳率:54%    等级:5 已帮助:205人 Android 隐藏虚拟按键,可以使用谷歌官方提供的api里的SYSTEM_UI_FLAG_HIDE_N ...

最新文章

  1. mysql聚合函数count用法_MySQL中聚合函数count的使用和性能优化技巧
  2. 【通知】有三AI新手入门群开放,欢迎新手们来加入
  3. 【温故知新】CSS学习笔记(字体样式属性)附加篇
  4. 理解ResNet结构与TensorFlow代码分析
  5. Windows上的单个进程所能访问的最大内存量是多少?它与系统的最大虚拟内存一样吗?这对于系统设计有什么影响?...
  6. 信息安全系统设计基础第三周学习总结—20135227黄晓妍
  7. JFrog Container Registry 搭建Docker镜像仓库 (tar.gz 版本)
  8. xpath安装与下载
  9. 建立Socket Policy服务器
  10. 毕设题目:Matlab图像增强
  11. ArcGIs创建企业级数据库
  12. 《第一行代码》第三版之我的第一行Android代码(一)
  13. mysql 计算农历_公历转换农历算法
  14. make: texi2dvi command not found 解决方法
  15. 联合主键是什么意思,联合主键怎么创建
  16. MySQL数据库触发器讲解与案例
  17. 努比亚红魔6spro线刷9008救砖教程
  18. 骑行中央公园,探索纽约“后花园”别样的美
  19. uniapp 微信分享(安卓)
  20. 中国申请英国留学人数创新高,东伦敦成最受家长青睐“以房养学”地区

热门文章

  1. redis实现轮询算法_白话分布式系统中的一致性哈希算法
  2. 01背包问题【回溯法求解】通俗易懂,适合小白
  3. 全球十佳电影,你看过几部
  4. 电子数据取证Windows取证读书笔记
  5. unity使用ugui自制调色面板
  6. 非计算机专业可以考的证书有哪些科目,证书可以免考自考非计算机专业中的“计算机应用”科目吗?...
  7. epson r330语言设置_爱普生打印机使用方法 爱普生r330打印机故障
  8. 使用Ngrok配置免费的外网域名
  9. 高校微信小程序开发心得
  10. 关于plist文件存储方式(swift)