Android软键盘之代码改变软键盘状态
16lz
2021-01-23
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 开发中,pullToRefreshListView 的刷新,加载
- Android Studio 添加百分比布局库的依赖报错 第一行代码
- 简单布局收缩动画
- Android C++ 动态加载so
- android中setImageResource加载大图片出现oom解决方案
- Android 基础布局控件自定义view使用练习
- Android加载Gif动画实现代码