Android 如何监听输入法关闭事件
假设有如下界面(输入法的上面的输入区域是用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 如何监听输入法关闭事件的更多相关文章
- Java面板Panel的使用,监听窗口关闭事件
面板Panel的使用 待解决问题: 1.设计模式:适配器模式 2.frame.setLayout(null); package GUI; import javax.swing.*; import ja ...
- Android之——监听手机开机事件
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47028535 本文中,主要通过监听开机广播来达到监听手机开机状态的操作.在Andr ...
- js监听浏览器关闭事件
html : <HTML> <HEAD> <title>test</title> </HEAD> <body onbefore ...
- Android实现监听控件点击事件
Android实现监听控件点击事件 引言 这篇文章主要想写一下Android实现监听点击事件的几种方法,Activity和Fragment实现起来有些方法上会有些不同,这里也略做介绍. 最近一直在忙一 ...
- Android 监听 Android中监听系统网络连接打开或者关闭的实现代码
本篇文章对Android中监听系统网络连接打开或者关闭的实现用实例进行了介绍.需要的朋友参考下 很简单,所以直接看代码 复制代码 代码如下: package xxx; import android.c ...
- Android监听点击事件实现的三种方法
监听点击事件实现的三种方法:1.匿名内部类2.外部类3.直接实现接口 1.匿名内部类: package com.jereh.calculator; import android.content.Con ...
- Android如何监听蓝牙耳机的按键事件
写在前面: 直接想要代码很简单,你直接把滚动条拉到最底端就可以看到.如果想要十分地了解为什么,那就按照我规划的一步一步来理解.以下测试环境以手头上有的「Bluedio + 红米手机」. 1.蓝牙耳机的 ...
- Android如何监听蓝牙耳机的按键事件(转)
源: Android如何监听蓝牙耳机的按键事件 写在前面: 直接想要代码很简单,你直接把滚动条拉到最底端就可以看到.如果想要十分地了解为什么,那就按照我规划的一步一步来理解.以下测试环境以手头上有的「 ...
- xamarin android如何监听单击事件
在xamarin android单击事件是最基础的事情,看过菜鸟上的android教程时,java写的都是监听事件,为一个按钮,单选按钮.多选按钮的单击事件有三种,前面两种用的非常普遍,也很简易,我这 ...
随机推荐
- [LeetCode] Task Scheduler 任务行程表
Given a char array representing tasks CPU need to do. It contains capital letters A to Z where diffe ...
- [BZOJ]1069: [SCOI2007]最大土地面积
题目大意:给出二维平面上n个点,求最大的由这些点组成的四边形面积.(n<=2000) 思路:求出凸包后旋转卡壳枚举对踵点对作为四边形的对角线,枚举或二分另外两个点,复杂度O(n^2)或O(nlo ...
- Sql Server 镜像相关
http://blog.csdn.net/dba_huangzj/article/details/35995083
- const的一些用法和理解
首先先说一下const常量的用处,我们知道宏定义#define是没有数据类型的,编译器在编译的时候,不会对宏常量进行类型检查,只进行简单的字符串替换,字符串替换时极易产生意想不到的错误,所以这个时候, ...
- Linux编程之内存池的设计与实现(C++98)
假设服务器的硬件资源"充裕",那么提高服务器性能的一个很直接的方法就是空间换时间,即"浪费"服务器的硬件资源,以换取其运行效率.提升服务器性能的一个重要方法就是 ...
- 阿里Java研发工程师实习面经
十分幸运 拿到阿里云的offer,感谢周围无数人对我的支持和鼓励,所以写篇面经希望可以帮助大家. 面试中,运气占很大一部分的,所以你们若是没有通过,一定不要气馁,继续加油. 每个努力的人 都值得钦佩, ...
- node之子线程child_process模块
node.js是基于单线程模型架构,这样的设计可以带来高效的CPU利用率,但是无法却利用多个核心的CPU,为了解决这个问题,node.js提供了child_process模块,用于新建子进程,子进程的 ...
- Linux中Mysql root用户看不到mysql库问题解决方式
第一种方式: 1.首先停止MySQL服务:service mysqld stop2.加参数启动mysql:/usr/bin/mysqld_safe --skip-grant-tables & ...
- python学习之路基础篇(第五篇)
前四天课程回顾 1.python简介 2.python基本数据类型 类: int:整型 | str:字符串 | list:列表 |tuple:元组 |dict:字典 | set:集合 对象: li = ...
- python序列化pickle/cPickle
一.pickle/Cpickle简介 Python序列化的概念很简单.内存里面有一个数据结构,你希望将它保存下来,重用,或者发送给其他人.你会怎么做?这取决于你想要怎么保存,怎么重用,发送给谁.很多游 ...