zoukankan      html  css  js  c++  java
  • Android 隐藏、显示软键盘方法

    隐藏软键盘的终极方法:

    public class SoftKeyboardUtil {
    
        /**
         * 隐藏软键盘(只适用于Activity,不适用于Fragment)
         */
        public static void hideSoftKeyboard(Activity activity) {
            View view = activity.getCurrentFocus();
            if (view != null) {
                InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
                inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    
        /**
         * 隐藏软键盘(可用于Activity,Fragment)
         */
        public static void hideSoftKeyboard(Context context, List<View> viewList) {
            if (viewList == null) return;
    
            InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
    
            for (View v : viewList) {
                inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

    那 SoftKeyboardUtil 第二个方法的 List<View> viewList 参数是什么, viewList 中需要放的是当前界面所有触发软键盘弹出的控件。 比如一个登陆界面, 有一个账号输入框和一个密码输入框, 需要隐藏键盘的时候, 就将两个输入框对象放在 viewList 中, 作为参数传到 hideSoftKeyboard 方法中即可。

    如下方法会弹出的隐藏,隐藏的弹出

    public static void hideKeyboard() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    详细见API:

    android-sdksourcesandroid-26androidviewinputmethodInputMethodManager.java


    Android 手动显示和隐藏软键盘

    https://blog.csdn.net/changsimeng/article/details/72853760

    1、方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示)

    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); 

    2、方法二(view为接受软键盘输入的视图,SHOW_FORCED表示强制显示)

    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
    imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);
    
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘

    3、调用隐藏系统默认的输入法

    ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this
    .getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); //(WidgetSearchActivity是当前的Activity)

    4、获取输入法打开的状态

    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
    boolean isOpen = imm.isActive();//isOpen若返回true,则表示输入法打开

  • 相关阅读:
    SpringBoot list查询方法
    eclipse创建web项目
    loadrunner获取返回值为乱码
    连接数据库
    lr并发量和迭代的区别
    LoadRunner11.00入门教程出现的问题
    python学习杂记--函数参数的设置
    adb logcat的命令行开启和关闭
    python学习杂记--pycharm控制台输出乱码
    python学习杂记--装饰器
  • 原文地址:https://www.cnblogs.com/bluestorm/p/8967492.html
Copyright © 2011-2022 走看看