Android 自用键盘操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | /** * 软键盘操作类 * 功能描述: * 1、打开软键盘 showSoftInput(EditText,Activity) or showSoftInput(Activity); * 2、关闭软键盘 closeSoftInput(EditText,Activity) or closeSoftInput(Activity); * Created by tab on 2018/05/04 14:10 */ public class KeyBoardUtils { /** * 打开软键盘 * * @param mEditText 输入框 * @param mContext 上下文 */ public static void showSoftInput(EditText mEditText, Context mContext) { InputMethodManager imm = (InputMethodManager) mContext .getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); } /** * 关闭软键盘 * * @param mEditText 输入框 * @param mContext 上下文 */ public static void closeSoftInput(EditText mEditText, Context mContext) { InputMethodManager imm = (InputMethodManager) mContext .getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0 ); } /** * 打开键盘. * * @param context the context */ public static void showSoftInput(Context context) { InputMethodManager inputMethodManager = (InputMethodManager) context .getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.toggleSoftInput( 0 , InputMethodManager.HIDE_NOT_ALWAYS); } /** * 关闭键盘事件. * * @param context the context */ public static void closeSoftInput(Context context) { InputMethodManager inputMethodManager = (InputMethodManager) context .getSystemService(Context.INPUT_METHOD_SERVICE); if (inputMethodManager != null && ((Activity) context).getCurrentFocus() != null ) { inputMethodManager.hideSoftInputFromWindow(((Activity) context).getCurrentFocus() .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } static int virtualKeyboardHeight; static int screenHeight; static int screenHeight6; static View rootView; /** * @param listener */ public static void setOnKeyboardChangeListener(Activity mContext, final KeyboardChangeListener listener) { screenHeight = mContext.getResources().getDisplayMetrics().heightPixels; screenHeight6 = screenHeight / 6 ; rootView = mContext.getWindow().getDecorView(); rootView.getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { //当键盘弹出隐藏的时候会 调用此方法。 @Override public void onGlobalLayout() { //回调该方法时rootView还未绘制,需要设置绘制完成监听 rootView.post( new Runnable() { @Override public void run() { Rect rect = new Rect(); //获取屏幕底部坐标 rootView.getWindowVisibleDisplayFrame(rect); //获取键盘的高度 int heightDifference = screenHeight - rect.bottom; if (heightDifference < screenHeight6) { virtualKeyboardHeight = heightDifference; if (listener != null ) { listener.onKeyboardHide(); } } else { if (listener != null ) { listener.onKeyboardShow(heightDifference - virtualKeyboardHeight); } } } }); } }); } /** * 软键盘状态切换监听 */ public interface KeyboardChangeListener { /** * 键盘弹出 * * @param keyboardHight 键盘高度 */ void onKeyboardShow( int keyboardHight); /** * 键盘隐藏 */ void onKeyboardHide(); } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
2021-05-27 MMKV使用笔记