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,则表示输入法打开

  • 相关阅读:
    Python 读取Excel之xlrd篇
    Python读取文件基本方法
    Python 偏函数用法全方位解析
    如何表示只有一个元素的元祖
    Python对文本读写的操作方法【源码】
    jvm入门及理解(六)——垃圾回收与算法
    jvm入门及理解(五)——运行时数据区(虚拟机栈)
    jvm入门及理解(四)——运行时数据区(堆+方法区)
    jvm入门及理解(三)——运行时数据区(程序计数器+本地方法栈)
    jvm入门及理解(二)——类加载器子系统
  • 原文地址:https://www.cnblogs.com/bluestorm/p/8967492.html
Copyright © 2011-2022 走看看