keyboard 动态启动或关闭软键盘

xiaoxiao2021-02-28  57

导读:

这里是一个关于Keyboard开启或关闭的工具类,方便平常直接拷贝用,就不赘述了

代码:

/* * @创建者 xmx * @博客 http://blog.csdn.net/u012792686 * @创建时间 2017/7/3 * @本类描述 软键盘开关工具类 * @内容说明 * * * @补充内容 * * --------------------------------- * @更新时间 * @新增内容 * */ public final class KeyboardToggleUtils { /* 避免输入法面板遮挡 <p>在manifest.xml中activity中设置</p> <p>android:windowSoftInputMode="adjustPan"</p> */ /** * 动态显示软键盘 * * @param activity activity */ public static void showSoftInput(Activity activity) { View view = activity.getCurrentFocus(); if (view == null) view = new View(activity); InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); if (imm == null) return; imm.showSoftInput(view, InputMethodManager.SHOW_FORCED); } /** * 动态显示软键盘 * * @param view 视图 */ public static void showSoftInput(View view) { view.setFocusable(true); view.setFocusableInTouchMode(true); view.requestFocus(); InputMethodManager imm = (InputMethodManager) Utils.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); if (imm == null) return; imm.showSoftInput(view, InputMethodManager.SHOW_FORCED); } /** * 动态隐藏软键盘 * * @param activity activity */ public static void hideSoftInput(Activity activity) { View view = activity.getCurrentFocus(); if (view == null) view = new View(activity); InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); if (imm == null) return; imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } /** * 动态隐藏软键盘 * * @param view 视图 */ public static void hideSoftInput(View view) { InputMethodManager imm = (InputMethodManager) Utils.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); if (imm == null) return; imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } /** * 切换键盘显示与否状态 */ public static void toggleSoftInput() { InputMethodManager imm = (InputMethodManager) Utils.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); if (imm == null) return; imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); } }
转载请注明原文地址: https://www.6miu.com/read-75024.html

最新回复(0)