<Android>监听软键盘打开收起事件(软键盘自带收起按钮)
2017-11-09 11:05
621 查看
Android输入框控件,遇到软键盘的事件监听,通常软键盘的收起方式大致3种:
1.点击软键盘右下角的Return按钮(系统收起)
2.输入框焦点时按返回按钮(系统收起)
3.点击软键盘和输入框的外部(自发收起)
4.点击软键盘自带的收起按钮(软键盘收起)
前三种事件可以监听,方式都比较简单
1.点击软键盘右下角的Return按钮
给输入框设置监听
2.输入框焦点时按返回按钮
给输入框增加按钮监听
3.点击软键盘和输入框的外部
给输入框的父容器增加触摸监听
4.点击软键盘自带的收起按钮(软键盘收起)
问题卡在此处,经过资料的搜查,还是么能找到软键盘收起按钮事件监听的办法,最后在stackoverflow网站找到了从布局高度的变化来判断软键盘的打开和收起事件。
[java] view plain copy
代码的使用:
地址链接:http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android
从中选取了这段代码。
这样能一并解决以上4种情况,比较好的监听软键盘的打开和收起事件,目前正在使用中。
原文地址: http://blog.csdn.net/xcookies/article/details/43024911
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网站找到了从布局高度的变化来判断软键盘的打开和收起事件。
[java] view plain copy
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 copy final SoftKeyboardStateHelper softKeyboardStateHelper = new SoftKeyboardStateHelper(findViewById(R.id.activity_main_layout); softKeyboardStateHelper.addSoftKeyboardStateListener(...); // then just handle callbacks SoftKeyboardStateHelper softKeyboardStateHelper = new SoftKeyboardStateHelper(findViewById(R.id.activity_main_layout)); softKeyboardStateHelper.addSoftKeyboardStateListener(new SoftKeyboardStateHelper.SoftKeyboardStateListener() { @Override public void onSoftKeyboardOpened(int keyboardHeightInPx) { //键盘打开 } @Override public void onSoftKeyboardClosed() { //键盘关闭 } });
地址链接:http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android
从中选取了这段代码。
这样能一并解决以上4种情况,比较好的监听软键盘的打开和收起事件,目前正在使用中。
原文地址: http://blog.csdn.net/xcookies/article/details/43024911
相关文章推荐
- <Android>监听软键盘打开收起事件(软键盘自带收起按钮)
- <Android>监听软键盘打开收起事件(软键盘自带收起按钮)
- <Android>监听软键盘打开收起事件(软键盘自带收起按钮)
- Android监听软键盘打开收起事件(软键盘自带收起按钮)
- Android监听软键盘打开收起事件(软键盘自带收起按钮)
- Android监听软键盘打开收起事件
- android监听键盘打开收起事件
- Android软键盘(七)键盘显示的状态下,onKeyDown方法无法监听到back事件
- Android软键盘(七)键盘显示的状态下,onKeyDown方法无法监听到back事件
- Android 另类方法监听软键盘的弹出收起事件
- android自定义软键盘名字以及监听键盘事件
- android 监听软键盘的收起与打开
- Android项目类似淘宝 电商 搜索功能,监听软键盘搜索事件,延迟自动搜索,以及时间排序的搜索历史记录的实现
- android 软键盘弹起监听事件
- 解决Android设置软键盘搜索键以及监听搜索键点击时发生两次事件的问题
- android 软键盘弹出移动view 键盘监听兼容横竖屏切换
- Android项目类似淘宝 电商 搜索功能,监听软键盘搜索事件,延迟自动搜索,以及时间排序的搜索历史记录的实现
- android studio开发<一> 按钮事件
- android 自定义View在弹出的软键盘之上,以及测量软键盘高度和监听软键盘状态
- Android按钮点击监听事件的写法