Android 软件键盘管理(显示或隐藏)

最近接触到PDA设备相关的业务,应用场景如下:
1.PDA机器是通过扫描接收参数
2.需要输入框获取到焦点,才能接收参数
3.输入框聚焦,但键盘弹出遮挡屏幕
4.默认输入框聚焦,但不要弹出软键盘,由按钮控制显示隐藏

开始代码工作
start-------------------------------------------------------------------------


import android.app.Activity;
import android.graphics.Rect;
import android.os.Build;
import android.text.InputType;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import java.lang.reflect.Method;

/**
 * 软件键盘管理
 * 如果需要按钮控制键盘显示或隐藏状态,前提条件必须要有焦点,不然则无效
 */
public class SoftKeyboardUtils {


    /**
     * 键盘状态
     *
     * @param activity
     * @return true显示 false隐藏
     */
    public static boolean getSoftKeyboardStatus(Activity activity) {
        //获取当前屏幕内容的高度
        int screenHeight = activity.getWindow().getDecorView().getHeight();
        //获取View可见区域的bottom
        Rect rect = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        return screenHeight - rect.bottom != 0;
    }

    /**
     * 根据键盘状态显示或隐藏键盘
     *
     * @param activity
     */
    public static void showOrHideSoftKeyboard(Activity activity) {
        if (getSoftKeyboardStatus(activity)) {
            hideKeyboard(activity);
        } else {
            showSoftKeyboard(activity);
        }
    }

    /**
     * 软键盘显示
     *
     * @param activity
     */

    public static void showSoftKeyboard(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        View view = activity.getCurrentFocus();
        if (view == null) {
            view = new View(activity);
        }
        imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
    }

    /**
     * 软键盘隐藏
     */
    public static void hideKeyboard(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        View view = activity.getCurrentFocus();
        if (view == null) {
            view = new View(activity);
        }
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    /**
     * edit获取焦点但不显示软键盘
     *
     * @param editText
     */
    public static void disableShowInput(EditText editText) {
        if (Build.VERSION.SDK_INT <= 10) {
            editText.setInputType(InputType.TYPE_NULL);
        } else {
            Class<EditText> cls = EditText.class;
            Method method;
            try {
                method = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
                method.setAccessible(true);
                method.invoke(editText, false);
            } catch (Exception e) {//TODO: handle exception
            }
        }
    }
}

以上是全部代码实现

补充说明:
如果要控制软键盘显示的前提条件是,必须先获取到焦点。

可以看官方源码
在这里插入图片描述
我们只需要看这段就可以,可以看到有两个参数,我们只看view参数,
view The currently focused view, which would like to receive
大概意思是说,视图已经获取到了聚焦,我需要从这里打开软键盘。所以这里打开软键盘这个动作,是建立在这个view视图已聚焦的基础之上的。
所以如果你未获取到焦点,是不可能打开软键盘的。

end--------------------------------------------------------------------------
本人菜鸟一枚,不足之处,还望指点

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android的Dialog中关闭软键盘需要以下步骤: 1. 首先,获取Dialog的Window对象。可以通过`dialog.getWindow()`方法来实现。 2. 接下来,调用Window的`setSoftInputMode()`方法,将其参数设置为`WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN`。这将会使得软键盘始终不会自动弹出。 3. 最后,调用Window的`setContentView()`方法加载布局文件或者视图对象。 代码示例: ```java Dialog dialog = new Dialog(context); Window window = dialog.getWindow(); window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); window.setContentView(R.layout.dialog_layout); ``` 在Android的Dialog中控制软键盘显示隐藏,可以通过以下方式实现: 1. 如果Dialog中有EditText控件需要获取焦点时,可以使用`InputMethodManager`类来显示键盘。可以在控件获得焦点时,调用`InputMethodManager`的`showSoftInput()`方法来显示键盘。 代码示例: ```java EditText editText = dialog.findViewById(R.id.edit_text); editText.requestFocus(); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); ``` 2. 如果需要在特定情况下隐藏键盘,可以使用`InputMethodManager`类的`hideSoftInputFromWindow()`方法。 例如,当失去焦点或者点击Dialog外部区域时,可以调用`hideSoftInputFromWindow()`方法来隐藏键盘。 代码示例: ```java View rootView = dialog.findViewById(R.id.root_view); rootView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } return false; } }); ``` 通过以上步骤,可以在Android的Dialog中关闭软键盘以及控制软键盘显示隐藏

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值