假设有如下界面(输入法的上面的输入区域是用Dialog实现的)

要求当输入法关闭的时候,Dialog也一起关闭,这样用户就不需要返回两次了。

网上找了很多资料都没有很好的解决这个问题,输入法是第三方程序,确实不好检测它的关闭与显示。

后来在EditText源码中看到


public boolean onKeyPreIme(int keyCode, KeyEvent event){

}

对该方法官方文档:

Handle a key event before it is processed by any input method associated with the view hierarchy. This can be used to intercept key events in special situations before the IME consumes them; a typical example would be handling the BACK key to update the application’s UI instead of allowing the IME to see it and close itself.

特别是最后一句话,该方法可以用来处理返回键来更新程序的UI。

所以我们监听通过 onKeyPreIme 的返回键,来关闭Dialog。


public class MyEditText extends EditText {

    public MyEditText(Context context) {
        super(context);
    }

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

    public MyEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public interface BackListener {
        void back(TextView textView);
    }

    private BackListener listener;

    public void setBackListener(BackListener listener) {
        this.listener = listener;
    }

    @Override

    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (listener != null) {
                listener.back(this);
            }
        }
        return false;
    }
}

在布局中使用我们自定义的EditText来代替系统的EditText:


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:text="长"
                android:textColor="#333"
                android:textSize="14sp"/>

            <MyEditText
                android:id="@+id/et_length"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:background="@null"
                android:gravity="center"
                android:hint="0cm"
                android:inputType="number"
                android:maxLines="5"
                android:textColorHint="#ccc"/>
        </LinearLayout>

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/divider_line"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:text="宽
                android:textColor="#333"
                android:textSize="14sp"/>

            <MyEditText
                android:id="@+id/et_width"
                android:layout_width="match_parent"
                android:layout_height="wrap_content
                android:layout_marginLeft="10dp"
                android:background="@null"
                android:gravity="center"
                android:hint="0cm"
                android:inputType="number"
                android:maxLines="5"
                android:textColorHint="#ccc"/>
        </LinearLayout>
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/divider_line"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:text="高"
                android:textColor="#333"
                android:textSize="14sp"/>

            <MyEditText
                android:id="@+id/et_height"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:background="@null"
                android:gravity="center"
                android:hint="0cm"
                android:inputType="number"
                android:maxLines="5"
                android:textColorHint="#ccc"/>
        </LinearLayout>

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/divider_line"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal"/>

    </LinearLayout>
</LinearLayout>

然后在Activity实现该接口,并且调用back方法


 @Override

 public void back(TextView textView) {
       if (sizeDialog != null && sizeDialog.isShowing()) {
                sizeDialog.dismiss();
        }
 }

private void initViews(){
    MyEditText etLength = (MyEditText) sizeDialog.findViewById(R.id.et_length);
    etLength.setBackListener(this);
    MyEditText etWidth = (MyEditText) sizeDialog.findViewById(R.id.et_width);
    etWidth.setBackListener(this);
    MyEditText etHeight = (MyEditText) sizeDialog.findViewById(R.id.et_height);
    etHeight.setBackListener(this);
}

最后运行效果,按返回键,键盘和Dialog都消失了:

Android 如何监听输入法关闭事件的更多相关文章

  1. Java面板Panel的使用,监听窗口关闭事件

    面板Panel的使用 待解决问题: 1.设计模式:适配器模式 2.frame.setLayout(null); package GUI; import javax.swing.*; import ja ...

  2. Android之——监听手机开机事件

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47028535 本文中,主要通过监听开机广播来达到监听手机开机状态的操作.在Andr ...

  3. js监听浏览器关闭事件

    html : <HTML>  <HEAD>  <title>test</title>  </HEAD>  <body onbefore ...

  4. Android实现监听控件点击事件

    Android实现监听控件点击事件 引言 这篇文章主要想写一下Android实现监听点击事件的几种方法,Activity和Fragment实现起来有些方法上会有些不同,这里也略做介绍. 最近一直在忙一 ...

  5. Android 监听 Android中监听系统网络连接打开或者关闭的实现代码

    本篇文章对Android中监听系统网络连接打开或者关闭的实现用实例进行了介绍.需要的朋友参考下 很简单,所以直接看代码 复制代码 代码如下: package xxx; import android.c ...

  6. Android监听点击事件实现的三种方法

    监听点击事件实现的三种方法:1.匿名内部类2.外部类3.直接实现接口 1.匿名内部类: package com.jereh.calculator; import android.content.Con ...

  7. Android如何监听蓝牙耳机的按键事件

    写在前面: 直接想要代码很简单,你直接把滚动条拉到最底端就可以看到.如果想要十分地了解为什么,那就按照我规划的一步一步来理解.以下测试环境以手头上有的「Bluedio + 红米手机」. 1.蓝牙耳机的 ...

  8. Android如何监听蓝牙耳机的按键事件(转)

    源: Android如何监听蓝牙耳机的按键事件 写在前面: 直接想要代码很简单,你直接把滚动条拉到最底端就可以看到.如果想要十分地了解为什么,那就按照我规划的一步一步来理解.以下测试环境以手头上有的「 ...

  9. xamarin android如何监听单击事件

    在xamarin android单击事件是最基础的事情,看过菜鸟上的android教程时,java写的都是监听事件,为一个按钮,单选按钮.多选按钮的单击事件有三种,前面两种用的非常普遍,也很简易,我这 ...

随机推荐

  1. [LeetCode] Longest Harmonious Subsequence 最长和谐子序列

    We define a harmonious array is an array where the difference between its maximum value and its mini ...

  2. [USACO12FEB]牛的IDCow IDs

    题目描述 Being a secret computer geek, Farmer John labels all of his cows with binary numbers. However, ...

  3. [BZOJ]3532: [Sdoi2014]Lis

    Time Limit: 10 Sec  Memory Limit: 512 MB Description 给定序列A,序列中的每一项Ai有删除代价Bi和附加属性Ci.请删除若干项,使得4的最长上升子序 ...

  4. DP测试总结

    T1:三取方格数 题目描述 设有N*N的方格图,我们将其中的某些方格填入正整数,而其他的方格中放入0.某人从图得左上角出发,可以向下走,也可以向右走,直到到达右下角.在走过的路上,他取走了方格中的数. ...

  5. UOJ #11. 【UTR #1】ydc的大树

    题目描述: ydc有一棵n个结点的黑白相间的大树,从1到n编号. 这棵黑白树中有m个黑点,其它都是白点. 对于一个黑点我们定义他的好朋友为离他最远的黑点.如果有多个黑点离它最远那么都是它的好朋友.两点 ...

  6. bzoj3309DZY Loves Math

    3309: DZY Loves Math Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1240  Solved: 777[Submit][Statu ...

  7. [Project] Simulate HTTP Post Request to obtain data from Web Page by using Python Scrapy Framework

    1. Background Though it's always difficult to give child a perfect name, parent never give up trying ...

  8. Python【第五课】迭代器,生成器,数据序列化

    本节内容 列表生成式,生成器,迭代器 Json & pickle 数据序列化 1.列表生成式,生成器,迭代器 1.1 列表生成式 列表生成式?不就是生成个列表的表达式,恩~~~ 差不多. 一般 ...

  9. Java利用Apache POI将数据库数据导出为excel

    将数据库中的数据导出为excel文件,供其他人查看 public class POITest { public static void main(String[] args) { POITest te ...

  10. 详解linux进程间通信-管道 popen函数 dup2函数

    前言:进程之间交换信息的唯一方法是经由f o r k或e x e c传送打开文件,或通过文件系统.本章将说明进程之间相互通信的其他技术-I P C(InterProcess Communication ...