android监听键盘弹出or隐藏方法
来源:互联网 发布:淘宝网冬天拖鞋 编辑:程序博客网 时间:2024/05/01 04:52
1.创建自定义KeyboardListenRelativeLayout,继承自RelativeLayout
public class KeyboardListenRelativeLayout extends RelativeLayout { private static final String TAG = KeyboardListenRelativeLayout.class.getSimpleName(); public static final byte KEYBOARD_STATE_SHOW = -3; public static final byte KEYBOARD_STATE_HIDE = -2; public static final byte KEYBOARD_STATE_INIT = -1; //true: show; false:hide private boolean lastStatus = false; private IOnKeyboardStateChangedListener onKeyboardStateChangedListener; public KeyboardListenRelativeLayout(Context context) { super(context); init(); } public KeyboardListenRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); init(); } public KeyboardListenRelativeLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } public void setOnKeyboardStateChangedListener(IOnKeyboardStateChangedListener onKeyboardStateChangedListener) { this.onKeyboardStateChangedListener = onKeyboardStateChangedListener; } public void init() { getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { private int screenheight = 0; public int getHeight() { if(screenheight>0) return screenheight; WindowManager wm = (WindowManager) getContext() .getSystemService(Context.WINDOW_SERVICE); //int width = wm.getDefaultDisplay().getWidth(); int height = wm.getDefaultDisplay().getHeight(); screenheight = height; return screenheight; } public void onGlobalLayout() { // TODO Auto-generated method stub Rect r = new Rect(); ((Activity)getContext()).getWindow().getDecorView().getWindowVisibleDisplayFrame(r); ; int screenHeight = getHeight(); int heightDiff = screenHeight - (r.bottom - r.top); Log.d("Keyboard Size", "Size: " + heightDiff); boolean visible = Math.abs(heightDiff) > screenHeight / 3; if(lastStatus!=visible) { lastStatus = visible; Log.d("Keyboard", "Keyboard " + (visible?"opened":"closed")); if(!visible&&onKeyboardStateChangedListener != null) { onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_HIDE); } if(visible&&onKeyboardStateChangedListener != null) { onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_SHOW); } } } }); } public interface IOnKeyboardStateChangedListener { public void onKeyboardStateChanged(int state); }}
2.配置到XML
<?xml version="1.0" encoding="utf-8"?><com.ziines.it.views.KeyboardListenRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:fresco="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/ll_chat_layout" >
3.Activity设置
mLl_chat_layout.setOnKeyboardStateChangedListener(new KeyboardListenRelativeLayout.IOnKeyboardStateChangedListener() { public void onKeyboardStateChanged(int state) { switch (state) { case KeyboardListenRelativeLayout.KEYBOARD_STATE_HIDE: NMGLog.i(TAG,"隐藏"); break; case KeyboardListenRelativeLayout.KEYBOARD_STATE_SHOW: NMGLog.i(TAG,"弹起"); break; default: break; } }});
0 0
- android监听键盘弹出or隐藏方法
- 监听android键盘弹出隐藏
- 监听键盘弹出/隐藏
- Android 键盘使用一(监听弹出隐藏)
- Android监听系统输入法键盘弹出显示与隐藏事件
- android监听键盘弹出
- 监听键盘的弹出和隐藏
- 安卓监听键盘弹出/隐藏
- Android 监听键盘弹出关闭
- Android监听键盘弹出收起
- android隐藏软键盘 不自动弹出键盘的方法
- 监听键盘弹出和隐藏,得到键盘高度
- Swift--监听iPhone键盘弹出及隐藏事件
- android 监听键盘弹出收起状态
- Android监听键盘的显示和隐藏
- android 键盘隐藏、显示的监听
- Android 键盘显示/隐藏监听事件
- Android监听键盘显示和隐藏
- Android屏幕适配经验谈(转)
- 佛祖class 注解
- GCC 6.1带来新的C++17特性、完全支持OpenMP、改进了OpenACC
- 征服 JavaScript 面试:什么是闭包?
- Netty学习前的知识储备
- android监听键盘弹出or隐藏方法
- 在.NET中如何使用RabbitMQ.
- Sublime Text3 常用快捷键
- Android Vr初识
- getOutputStream() has already been called for this response
- 应用子表时如何将开窗查询事件做成多选?
- VMware虚拟机桥接方式与真实主机共享上网
- Ajax使用jQuery与后台交互
- Cassandra3.9版本基础入门3-插入和查询