Android 控制软键盘显示、隐藏,并且添加软键盘的状态监听的终极解决办法
来源:互联网 发布:5s4g网络怎么设置 编辑:程序博客网 时间:2024/05/28 01:34
最近在开发中需要控制软键盘的显示隐藏,刚开始自定义了一个,感觉效果不是太理想,然后就写了一个软键盘的显示、隐藏的状态监听,特意在这里记录一下……
请看代码:
Step 1:
创建类
package xxx;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(); } } }}
Step 2:
在需要使用的类中,添加监听即可
类实现方法
implements SoftKeyboardStateHelper.SoftKeyboardStateListener {...}
final SoftKeyboardStateHelper softKeyboardStateHelper = new SoftKeyboardStateHelper(view.findViewById(R.id.iv_product_detail_calculate));
softKeyboardStateHelper.addSoftKeyboardStateListener(this);
@Overridepublic void onSoftKeyboardOpened(int keyboardHeightInPx) { if (!cDialog.isShowing()) { InputMethodManager imm = (InputMethodManager) getCurActivity().getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS); imm.showSoftInput(getView(), InputMethodManager.SHOW_FORCED); }}@Overridepublic void onSoftKeyboardClosed() {}
阅读全文
1 0
- Android 控制软键盘显示、隐藏,并且添加软键盘的状态监听的终极解决办法
- Android软键盘的隐藏显示监听
- android控制软键盘的隐藏显示
- android 监听判断软键盘的弹出、隐藏状态
- 监听软键盘的显示和隐藏
- 监听软键盘的显示隐藏
- 监听软键盘的显示与隐藏
- Android监听软键盘的显示与隐藏
- Android监听软键盘的显示与隐藏
- Android 监听软键盘的显示与隐藏
- android 监听软键盘的显示与隐藏
- Android 监听软键盘的显示与隐藏
- Android监听软键盘的显示与隐藏
- 监听软键盘的弹出和隐藏 ( 终极方法)
- Android 软键盘弹起隐藏的监听
- 控制软键盘的显示与隐藏
- 控制软键盘的显示和隐藏
- Android监听的软键盘的状态
- 每日一贴--2017/9/1
- SCI期刊图像处理
- 软件测试常见问题概念汇总(武林秘籍)
- post方式下载后台传来的文件
- Mysql根据坐标字符串排序
- Android 控制软键盘显示、隐藏,并且添加软键盘的状态监听的终极解决办法
- js实现h5滑动杆range功能
- 个人开发环境sudo+cscope+vim
- 笔试题
- Linux必备命令_测试01
- Tensorflow简单复现softmax模型对于mnist数据集分类
- Jenkins pipeline中下载多个配置库
- OOA/D:面向对象分析与设计
- BZOJ:2958 序列染色 DP