android代码控制软键盘的打开与关闭的工具类(KeybordS)

来源:互联网 发布:niconico网络连接失败 编辑:程序博客网 时间:2024/06/05 11:57

下面介绍一个我项目开发经常用到的一个已经封装好的,实用的工具类。

       功能:

               1.软键盘的打开与关闭

               2.判断当前软键盘是否打开

      源码下载地址:http://pan.baidu.com/s/1hsfXjfe

      源码:    

package com.beimu.soldier.common.util;import android.app.Activity;import android.content.Context;import android.view.View;import android.view.inputmethod.InputMethodManager;import android.widget.EditText;public class KeybordS {    /**     * 打开软键盘     *     * @param mEditText     * @param mContext     */    public static void openKeybord(EditText mEditText, Context mContext) {        InputMethodManager imm = (InputMethodManager) mContext                .getSystemService(Context.INPUT_METHOD_SERVICE);        imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,                InputMethodManager.HIDE_IMPLICIT_ONLY);    }    /**     * 关闭软键盘     *     * @param mEditText输入框     * @param mContext上下文     */    public static void closeKeybord(EditText mEditText, Context mContext) {        InputMethodManager imm = (InputMethodManager) mContext                .getSystemService(Context.INPUT_METHOD_SERVICE);        imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);    }    /**     * 判断当前软键盘是否打开     *     * @param activity     * @return     */    public static boolean isSoftInputShow(Activity activity) {        // 虚拟键盘隐藏 判断view是否为空        View view = activity.getWindow().peekDecorView();        if (view != null) {            // 隐藏虚拟键盘            InputMethodManager inputmanger = (InputMethodManager) activity                    .getSystemService(Activity.INPUT_METHOD_SERVICE);//       inputmanger.hideSoftInputFromWindow(view.getWindowToken(),0);            return inputmanger.isActive() && activity.getWindow().getCurrentFocus() != null;        }        return false;    }}

0 0