Android 软键盘的打开关闭监听
来源:互联网 发布:如何开通淘宝 编辑:程序博客网 时间:2024/05/20 21:21
软键盘的事件监听,通常软键盘的收起方式大致3种:
1.点击软键盘右下角的Return按钮(系统收起)
2.输入框焦点时按返回按钮(系统收起)
3.点击软键盘和输入框的外部(自发收起)
4.点击软键盘自带的收起按钮(软键盘收起)
前三种事件可以监听,方式都比较简单
1.点击软键盘右下角的Return按钮
给输入框设置监听
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event) {
// 可捕捉右下角的Return按钮
//添加抛出收起事件代码
return false;
}
});
2.输入框焦点时按返回按钮
给输入框增加按钮监听
editText.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (listener != null) {
// 可添加抛出收起事件代码
}
return true;
}
return false;
}
});
3.点击软键盘和输入框的外部
给输入框的父容器增加触摸监听
@Override
public boolean onTouchEvent(MotionEvent event) {
if (indexOfChild(editText) > -1) {
// 可添加抛出收起事件代码
}
return super.onTouchEvent(event);
}
4.点击软键盘自带的收起按钮(软键盘收起)
问题卡在此处,经过资料的搜查,还是么能找到软键盘收起按钮事件监听的办法,最后在stackoverflow网站找到了从布局高度的变化来判断软键盘的打开和收起事件。
import android.graphics.Rect; import android.view.View; import android.view.ViewTreeObserver; import java.util.LinkedList; import java.util.List; public class SoftKeyboardStateHelper implements ViewTreeObserver.OnGlobalLayoutListener { public interface SoftKeyboardStateListener { void onSoftKeyboardOpened(int keyboardHeightInPx); void onSoftKeyboardClosed(); } private final List<SoftKeyboardStateListener> listeners = new LinkedList<SoftKeyboardStateListener>(); private final View activityRootView; private int lastSoftKeyboardHeightInPx; private boolean isSoftKeyboardOpened; public SoftKeyboardStateHelper(View activityRootView) { this(activityRootView, false); } public SoftKeyboardStateHelper(View activityRootView, boolean isSoftKeyboardOpened) { this.activityRootView = activityRootView; this.isSoftKeyboardOpened = isSoftKeyboardOpened; activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this); } @Override public void onGlobalLayout() { final Rect r = new Rect(); //r will be populated with the coordinates of your view that area still visible. activityRootView.getWindowVisibleDisplayFrame(r); final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top); if (!isSoftKeyboardOpened && heightDiff > 100) { // if more than 100 pixels, its probably a keyboard... isSoftKeyboardOpened = true; notifyOnSoftKeyboardOpened(heightDiff); } else if (isSoftKeyboardOpened && heightDiff < 100) { isSoftKeyboardOpened = false; notifyOnSoftKeyboardClosed(); } } public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) { this.isSoftKeyboardOpened = isSoftKeyboardOpened; } public boolean isSoftKeyboardOpened() { return isSoftKeyboardOpened; } /** * Default value is zero (0) * @return last saved keyboard height in px */ public int getLastSoftKeyboardHeightInPx() { return lastSoftKeyboardHeightInPx; } public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) { listeners.add(listener); } public void removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) { listeners.remove(listener); } private void notifyOnSoftKeyboardOpened(int keyboardHeightInPx) { this.lastSoftKeyboardHeightInPx = keyboardHeightInPx; for (SoftKeyboardStateListener listener : listeners) { if (listener != null) { listener.onSoftKeyboardOpened(keyboardHeightInPx); } } } private void notifyOnSoftKeyboardClosed() { for (SoftKeyboardStateListener listener : listeners) { if (listener != null) { listener.onSoftKeyboardClosed(); } } } }
代码的使用:
[java] view plain copyfinal SoftKeyboardStateHelper softKeyboardStateHelper = new SoftKeyboardStateHelper(findViewById(R.id.activity_main_layout); softKeyboardStateHelper.addSoftKeyboardStateListener(...)
这样能一并解决以上4种情况,比较好的监听软键盘的打开和收起事件,目前正在使用中。
原文 :监听软键盘打开收起事件(软键盘自带收起按钮)
- Android 软键盘的打开关闭监听
- Android监听软键盘的关闭
- android软键盘的关闭监听
- Android 打开软键盘 关闭软键盘
- 监听android软键盘弹出或者关闭的方法
- Android学习之监听软键盘的弹起关闭
- Android监听手机软键盘的弹起和关闭
- Android 软键盘弹出与关闭监听
- android软键盘弹出关闭监听
- Android监听软键盘开启关闭状态
- Android监听软键盘打开收起事件
- Android开发 关于软键盘的 打开与关闭
- Android打开或者关闭软键盘的解决方案
- [Android] 软键盘打开与关闭的方法
- android中软键盘的打开关闭及判断操作
- 史上最全:安卓监听软键盘打开或者关闭
- 监听软键盘打开或关闭触发动作
- Android自动打开和关闭软键盘
- linux ubuntu下使用ss设置全局代理,命令行也走代理
- Shiro入门学习四
- http、https 等 常用默认端口号
- 第十周项目一——验证算法之层序遍历算法的验证
- 【bzoj2527】[Poi2011]Meteors
- Android 软键盘的打开关闭监听
- set() get()
- SaltStack实战应用
- 我是如何向老婆解释MapReduce的?
- 括号匹配问题
- 第十周项目1-验证算法(4)
- angularJS 学习
- 第8周项目2-顺序串算法
- Android Studio快捷键