android监听软键盘的弹出与隐藏的示例代码

 更新时间:2017年02月22日 08:14:46   作者:山代  
本篇文章主要介绍了android监听软键盘的弹出与隐藏的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

情境:布局文件中有ScrollView,ScrollView中有个EditView,布局底部有一个控件(见下面布局代码),程序一启动EditView就获取焦点,弹出软键盘,将这个底部的控件也顶上去了,感觉不太好,所以我就想监听下软键盘弹出,此时去隐藏底部控件,软键盘隐藏时则显示底部控件。

初始:

这里写图片描述

这里写图片描述      这里写图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  android:id="@+id/activity_main"
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:fitsSystemWindows="true"
  tools:context="com.test.myapplication.MainActivity">

  <TextView
    android:layout_margin="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"/>

  <LinearLayout
    android:id="@+id/lin"
    android:background="#0000ff"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">
    <ScrollView
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <EditText
          android:layout_width="match_parent"
          android:layout_height="wrap_content"/>
      </LinearLayout>
    </ScrollView>
  </LinearLayout>
  <TextView
    android:layout_margin="10dp"
    android:id="@+id/txt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"/>
</LinearLayout>

Android api提供了使得软键盘的弹出与隐藏的方式,比如

if(getWindow().getAttributes().softInputMode==WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED)
    {
     //隐藏软键盘
      getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    }

但是并未提供监听软键盘的弹出与隐藏的方法。

由于弹出与隐藏软键盘势必会引起layout布局的变化,监听布局的变化然后计算偏移,即可算出是否时显示或隐藏,有两种解决方案。

1、自定义View,修改OnLayout()方法,比如

public class ResizeLayout extends LinearLayout {
  private InputListener mListener;

  public interface InputListener {
    void OnInputListener(boolean isHideInput);
  }

  public void setOnResizeListener(InputListener l) {
    mListener = l;
  }

  public ResizeLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  private boolean mHasInit = false;
  private boolean mHasKeyboard = false;
  private int mHeight;

  @Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    // TODO Auto-generated method stub
    super.onLayout(changed, l, t, r, b);
    if (!mHasInit) {
      mHasInit = true;
      mHeight = b;
      System.out.println("mHeight= " + b);
    }
    else {
      mHeight = mHeight < b ? b : mHeight;
    }

    if (mHasInit && mHeight > b) { // mHeight代表键盘的真实高度 ,b代表在窗口中的高度 mHeight>b
      mHasKeyboard = true;
      mListener.OnInputListener(false);
    }
    if (mHasInit && mHasKeyboard && mHeight == b) { // mHeight = b
      mHasKeyboard = false;
      mListener.OnInputListener(true);
    }
  }

2、在activity中获取ViewGroup的高度变化

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /*// 隐藏标题栏
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    // 隐藏状态栏
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);*/
    setContentView(R.layout.activity_main);
    final LinearLayout lin = (LinearLayout) findViewById(R.id.lin);
    final TextView txt = (TextView) findViewById(R.id.txt);

    lin.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
        Rect rect = new Rect();
        lin.getWindowVisibleDisplayFrame(rect);
        int rootInvisibleHeight = lin.getRootView().getHeight() - rect.bottom;
        Log.d(TAG, "lin.getRootView().getHeight()=" + lin.getRootView().getHeight() + ",rect.bottom=" + rect.bottom + ",rootInvisibleHeight=" + rootInvisibleHeight);
        if (rootInvisibleHeight <= 100) {
        //软键盘隐藏啦
          txt.postDelayed(new Runnable() {
            @Override
            public void run() {
              txt.setVisibility(View.VISIBLE);
            }
          },100);
        } else {
          ////软键盘弹出啦
          txt.setVisibility(View.GONE);
        }
      }
    });
  }

题外话:测试时发现通过设置全屏getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);也可以达到相同目的,但是全屏就违背了我的初衷。个人推荐第二种方法,因为遇到一个客户的设备在开启指纹识别的相册锁时,第一种方法不好使。

在查资料的过程中看到有些开发者希望软键盘弹出时把底部控件顶上去的情形,方法同上。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android控件之RatingBar自定义星级评分样式

    Android控件之RatingBar自定义星级评分样式

    RatingBar为评分条控件,默认效果为若干个绿色的星星,如果想将其换成其他自定义图片就要自定义它的style。接下来通过本文给大家介绍Android控件之RatingBar自定义星级评分样式,感兴趣的朋友一起学习吧
    2016-02-02
  • Android 的触摸事件详解及示例代码

    Android 的触摸事件详解及示例代码

    本文主要介绍Android 的触摸事件,这里整理了详细的资料,并附代码示例,希望能帮助到有需要的小伙伴
    2016-09-09
  • Android 5.0 开机横屏修改方法

    Android 5.0 开机横屏修改方法

    这篇文章主要介绍了 Android 5.0 开机横屏修改方法,大概可以分为三部分,具体修改方法,大家参考下本文
    2017-01-01
  • Android中应用前后台切换监听的实现详解

    Android中应用前后台切换监听的实现详解

    这篇文章主要给大家介绍了关于Android中应用前后台切换监听实现的相关资料,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习学习吧。
    2017-07-07
  • Android Handler消息派发机制源码分析

    Android Handler消息派发机制源码分析

    这篇文章主要为大家详细分析了Android Handler消息派发机制源码,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • Android多渠道打包时获取当前渠道的方法

    Android多渠道打包时获取当前渠道的方法

    这篇文章主要介绍了Android多渠道打包时获取当前渠道的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01
  • Angular5.0.0新特性

    Angular5.0.0新特性

    Angular5.0.0是一款非常优秀的前端JS框架,已经被用于google多款产品当中,这篇文章主要介绍了Angular5.0.0新特性,需要的朋友可以参考下
    2017-11-11
  • Android三种常见的图片压缩方式

    Android三种常见的图片压缩方式

    在开发中,我们经常有这样一种需求,从相册选择图片,上传到服务器。随着手机像素的不断提升,照片也是小一点的3,4兆,大一点的多大10多兆。如果直接上传原图,会增大服务器压力,所以,在上传之前对图片压缩就显得很必要了。
    2021-05-05
  • Android 标准Intent的使用详解

    Android 标准Intent的使用详解

    这篇文章主要介绍了Android 标准Intent的使用详解的相关资料,需要的朋友可以参考下
    2017-03-03
  • Android编程设计模式之工厂方法模式实例详解

    Android编程设计模式之工厂方法模式实例详解

    这篇文章主要介绍了Android编程设计模式之工厂方法模式,结合实例形式详细分析了Android工厂方法模式的概念、原理、使用方法及相关注意事项,需要的朋友可以参考下
    2017-12-12

最新评论