Android软键盘之代码改变软键盘状态
16lz
2022-06-15
Android中软键盘的管理主要是通过InputMethodManager来完成的,
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
使用方法:
import android.app.Activity;import android.content.Context;import android.view.inputmethod.InputMethodManager;import android.widget.EditText;/** * Created by Administrator on 2017/4/4. * 软键盘工具类 */public class KeyboardUtil { /** * 显示软键盘,当布局加载完成后调用,否则无效 * @param context * @param focusView:必须为EditText或者其子类并且获得焦点,并且是VISIBLE */ public static void showSoftInput(Context context, EditText focusView){ InputMethodManager inputMethodManager= (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); if (inputMethodManager != null) { focusView.requestFocus(); inputMethodManager.showSoftInput(focusView,0); } } /** * 隐藏软键盘,当布局加载完成后调用,否则无效 * @param context */ public static void hideSoftInput(Context context){ InputMethodManager inputMethodManager= (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); if (inputMethodManager != null) { inputMethodManager.hideSoftInputFromWindow(((Activity)context).getWindow().getDecorView().getWindowToken(),0); } } /** * 切换软键盘状态(隐藏-显示或显示-隐藏),当布局加载完成后调用,否则无效 * @param context */ public static void toggleSoftInput(Context context){ InputMethodManager inputMethodManager= (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); if (inputMethodManager != null) { inputMethodManager.toggleSoftInput(0,0); } }}
参考博客: Android手动显示和隐藏软键盘
更多相关文章
- Android弹出软键盘布局是否上移问题
- android 属性android:visibility
- Android的onCreateOptionsMenu()创建菜单Menu详解
- Android(安卓)动画显示文字与bitmap的BadgeView
- 2010.12.16——— android listView 显示图片 内存溢出问题
- Android(安卓)UI界面基本属性 大全
- Android(安卓)软键盘盖住输入框的问题
- RecyclerView不显示问题
- 设置屏幕显示模式ScreenOrientation.