有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java编程实现软键盘的切换隐藏

我们有一个在安卓设备(ScanSKU)上运行的应用程序。每当用户扫描任何内容时,键盘都会显示为“键入”他们扫描的文本,并将屏蔽屏幕的一半

我们的应用程序基本上是一个网络视图,我们仍然需要能够聚焦其中的表单元素

但是,我们是否可以构建一个切换来禁止屏幕上的键盘在WebView中出现

我试过用这个:

// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {  
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

发件人:Close/hide the Android Soft Keyboard

但没有成功

关于我该怎么做,有什么建议或想法吗


共 (1) 个答案

  1. # 1 楼答案

    还有另一种获取焦点视图的方法,getCurrentFocus方法对我也不适用。尝试这个解决方案,你应该提供活动对象的实例

    public static void hideSoftKeyboard(Context context) {
        try {
            InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(((Activity) context).getWindow().getDecorView().getRootView().getWindowToken(), 0);
            ((Activity) context).getWindow().getDecorView().getRootView().clearFocus();
        } catch (NullPointerException e) {
            // some error log 
        }
    }
    

    编辑:如果您使用WebView,并且仍然需要能够聚焦其中的表单元素。可以设置属性

    view.setFocusableInTouchMode(false);
    

    另一方面,屏幕上的视图和WebView将关注它