Android 如何监听输入法关闭事件
2016-05-21 18:42
465 查看
假设有如下界面(输入法的上面的输入区域是用Dialog实现的)
![](http://img.blog.csdn.net/20160521183105419)
要求当输入法关闭的时候,Dialog也一起关闭,这样用户就不需要返回两次了。
网上找了很多资料都没有很好的解决这个问题,输入法是第三方程序,确实不好检测它的关闭与显示。
后来在EditText源码中看到
对该方法官方文档:
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。
所以我们监听通过
在布局中使用我们自定义的EditText来代替系统的EditText:
然后在Activity实现该接口,并且调用back方法
最后运行效果,按返回键,键盘和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】ImageView的src和background的区别以及两者的妙用
- 【oschina android源码分析】总结
- 【oschina android源码分析】聊天页面(私信)的设计
- 【oschina android源码分析】便笺系统的设计
- 【oschina android源码分析】下载更新新版本的设计
- Android 多个Edittext不为空,Button才可点击的方法
- Android数据过滤器:Filter
- 【oschina android源码分析】切换夜间模式的实现
- 【oschina android源码分析】页面通知(站内信)的设计-android轮询方案
- Android 编程下的 TraceView 简介及其案例实战
- 详解Android动画之Frame Animation
- 【oschina android源码分析】登陆和退出的设计
- 第一章 JAVA入门(Android历史版本续1)
- Android Reference官方描述
- 第一章 JAVA入门(Android历史版本续3)
- 第一章 JAVA入门(Android 历史版本续2)
- Android中Preference的使用以及监听事件分析(自己学习)
- Installation error: INSTALL_FAILED_CONFLICTING_PROVIDER 解决方案-Android
- android 4.2上面实现录制系统内置声音remote submix
- 图片选择------本地相册