Android6.0的坑之webview的showFindDialog

在Android 6.0中,Webview的showFindDialog方法存在一个问题:在焦点模式下,无法通过OK键弹出软键盘。本文探讨了这个问题的根源,通过查看Android 4.0的实现来寻找解决方案,最终通过修改FindActionModeCallback中的点击监听解决了软键盘弹出问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android的webview有个系统弹窗的功能是,可以出现搜索匹配网页内字符串关键字的功能。其实就是这个方法:

    /**
     * Starts an ActionMode for finding text in this WebView.  Only works if this
     * WebView is attached to the view system.
     *
     * @param text if non-null, will be the initial text to search for.
     *             Otherwise, the last String searched for in this WebView will
     *             be used to start.
     * @param showIme if true, show the IME, assuming the user will begin typing.
     *                If false and text is non-null, perform a find all.
     * @return true if the find dialog is shown, false otherwise
     * @deprecated This method does not work reliably on all Android versions;
     *             implementing a custom find dialog using WebView.findAllAsync()
     *             provides a more robust solution.
     */
    @Deprecated
    public boolean showFindDialog(String text, boolean showIme) {
        checkThread();
        return mProvider.showFindDialog(text, showIme);
    }

具体调用是webview. showFindDialog(null,true);默认弹出软键盘。但是有个bug就是如果是焦点模式,去使用这个弹窗的话,就不能按ok键触发弹出软键盘,只能搜索系统中出现的下一个字符串,造成我们这种智能电视项目只能用遥控器操作,就出现了问题,功能感觉不能正常使用。所以,当然就是找到showFindDialog的具体实现在哪里,然后确认editText的有  

        mEditText.setFocusable(true);
        mEditText.setFocusableInTouchMode(true);
        mEditText.requestFocus();
        mEditText.requestFocusFromTouch();


这几个属性。但是不论怎么全局搜索,都没有找到函数的实现体。后来绝望之下,查百度,发现大家都是在Android4.0左右的版本找到这个函数的实现,Android5.0以上就没有具体实现了,怎么办,问题是要解决啊,于是我灵机一动,想着android4.0上不是有实现体吗,看看里面具体是啥操作,一看里面也没有具体到editText这个控件。

WebViewChromium.java (frameworks\webview\chromium\java\com\android\webview\chromium) 68563 2016/12/13

有函数的具体实现

    @Override
    public boolean showFindDialog(final String text, final boolean showIme) {
        mFactory.startYourEngines(false);
        if (checkNeedsPost()) {
            return false;
        }
        if (mWebView.getParent() == null) {
            return false;
        }

        FindActionModeCallback findAction = new FindActionModeCallback(mWebView.getContext());
        if (findAction == null) {
            return false;
        }

        mWebView.startActionMode(findAction);
        findAction.setWebView(mWebView);
        if (showIme) {
            findAction.showSoftInput();
        }

        if (text != null) {
            findAction.setText(text);
            findAction.findAll();
        }

        return true;
    }



继续跟踪到FindActionModeCallback中,终于找到了edittext。好吧,死马当活马医,在Android6.0上竟然找到了这个FindActionModeCallback.java

按照原本思路加控件聚焦,运行,发现还是弹不出软键盘,再看

    

    public FindActionModeCallback(Context context) {
        mCustomView = LayoutInflater.from(context).inflate(
                com.android.internal.R.layout.webview_find, null);
        mEditText = (EditText) mCustomView.findViewById(
                com.android.internal.R.id.edit);
        mEditText.setFocusable(true);
        mEditText.setFocusableInTouchMode(true);
        mEditText.requestFocus();
        mEditText.requestFocusFromTouch();
        mEditText.setCustomSelectionActionModeCallback(new NoAction());
        //mEditText.setOnClickListener(this);
        setText("");
        mMatches = (TextView) mCustomView.findViewById(
                com.android.internal.R.id.matches);
        mInput = (InputMethodManager)
                context.getSystemService(Context.INPUT_METHOD_SERVICE);
        mResources = context.getResources();
        Log.e("YQL","==================FindActionModeCallback");
    }


是不是那个click监听导致失效啊,一看监听函数的实现,果然,里面就是搜寻下一个字符串,把弹软键盘的功能拦截了,果断屏蔽。。。

终于搞定一个bug,当然屏蔽的click并不会影响用户使用,因为对话窗口上还有进行上一个下一个搜索的控件。

整了两天,Android6.0的版本源码至今还是没找到showfinddialog的实现体,谁知道的话,能否告知。。。不胜感激。
       





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值