繁体   English   中英

在WebView中隐藏特定页面的键盘

[英]Hide keyboard in WebView for specific pages

我为工作创建了一个简单的WebView应用程序,以使工作人员可以填写表格。 他们必须在2点扫描条形码(数位板具有内置扫描仪)。

我的问题是,扫描仪需要聚焦在输入上,因此弹出软键盘,这在此时是不需要的,并且看起来有些混乱。

我尝试了在stackoverflow和google上找到的几种方法来隐藏键盘,但未成功。

关键是只在特定页面(使用扫描仪的页面)上隐藏键盘,而在需要其他页面的其他页面上仍然可用。

WebView的页面是用HTMLJS构建的。 因此,JS解决方案可能会起作用,但我希望使用真正的Java解决方案。

我当前使用的代码会检查url,并尝试隐藏键盘(不成功)。 如您所见,我已经尝试了几种解决方案,但没有一个可行。

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {

    super.onPageStarted(view, url, favicon);

    Log.d("WebView", "your current url when webpage loading.." + url);

    if(url.equals("https://********/index.php?show=kontrolle&step=4")) {
        this.activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        Log.d("WebView", "should hide now!");
        //view = activity.findViewById(android.R.id.content);
        //InputMethodManager imm =(InputMethodManager) this.activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        //imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        //this.activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        //this.activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

    }

}

在manifest.xml文件中,编写以下代码

<activity
        android:name=".activity.MainActivity"
        android:windowSoftInputMode="stateHidden" />

用这个

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//hide keboard:
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
//and for show keyboard
imm.showSoftInput(myEditText, InputMethodManager.SHOW_IMPLICIT);

您需要了解WebViewClient请访问此网址

1)首先,您需要添加WebViewClient,并且在更改页面时,您可以在shouldOverrideUrlLoading获取当前网址,该网址会检查当前网址并将其与您的网址进行比较并隐藏键盘

例如 :

WebView webview = new WebView(context);
webview.setWebViewClient(new WebViewClient()
        {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);

            Log.d("WebView", "your current url when webpage loading.." + url);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            Log.d("WebView", "your current url when webpage loading.. finish" + url);
            super.onPageFinished(view, url);
        }

        @Override
        public void onLoadResource(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onLoadResource(view, url);
        }
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            System.out.println("when you click on any interlink on webview that time you got url :-" + url);
//compare here your URL and hide keyboard
            return super.shouldOverrideUrlLoading(view, url);
        }
    });

隐藏软键盘的代码

这是隐藏软键盘的两种方法,您可以将它们保留在Common-Utils类中

public static void hideSoftKeyboard(Activity activity) {
    if (activity != null && activity.getCurrentFocus() != null) {
        InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);

    }

}

第二种方法(如果EditText可用)

public static void forceHideKeyboard(Activity activity, EditText editText) {
    try {

        if (editText == null) return;

        if (activity.getCurrentFocus() == null
                || !(activity.getCurrentFocus() instanceof EditText)) {
            editText.requestFocus();
        }

        InputMethodManager imm = (InputMethodManager) activity
                .getSystemService(INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
        activity.getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);


    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM