Android WebView findnext不会滚动到下一个选定的单词

时间:2016-07-07 19:51:37

标签: android webview

当我调用webView.findAll(word);时,它确实显示所有出现的选项,webView.findNext(true)也显示不同的背景颜色,但它不会将页面滚动到下一个选定的单词。

我必须手动滚动页面才能看到所选单词。我希望每当我打电话给webView.findNext(true)时,它应该自动滚动页面到下一个单词。我使用的是Android 6.0设备。我也试过了webView.findAllAsync(word);。但是我无法将webView页面滚动到所选单词。请帮忙!

编辑:

布局文件:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white" >

    <WebView android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tv_for_dummies"
        android:textColor="@android:color/black"
        android:padding="7dp"/>
</ScrollView>

menu.xml文件

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:appcompat="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/tools" >

    <item
        android:id="@+id/search"
        android:icon="@android:drawable/ic_menu_search"
        android:title="Search"
        appcompat:actionViewClass="android.support.v7.widget.SearchView"
        appcompat:showAsAction="ifRoom|collapseActionView"/>
    <item
        android:id="@+id/next"
        android:icon="@android:drawable/arrow_down_float"
        android:title="Next"
        android:visible="false"
        appcompat:showAsAction="always"/>
    <item
        android:id="@+id/prev"
        android:icon="@android:drawable/arrow_up_float"
        android:title="Previous"
        android:visible="false"
        appcompat:showAsAction="always"/>

</menu>

DummiesActivity.java文件

public class DummiesActivity extends AppCompatActivity {

    WebView wvDummies;
    Context context;
    Float scale = 0.5F;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.dummies_guide);

        wvDummies = (WebView) findViewById(R.id.tv_for_dummies);

        wvDummies.getSettings().setSupportZoom(true);
        wvDummies.getSettings().setBuiltInZoomControls(true);
        wvDummies.getSettings().setJavaScriptEnabled(true);
        wvDummies.loadData(getString(R.string.app_name123), "text/html",
                "UTF-8");

        getSupportActionBar().setTitle("Dummies guide");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        getMenuInflater().inflate(R.menu.dummies_search_menu, menu);

        SearchManager searchManager = (SearchManager) context
                .getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search)
                .getActionView();
        searchView.setSearchableInfo(searchManager
                .getSearchableInfo(getComponentName()));
        searchView.setQueryHint("Search");

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @SuppressLint("NewApi")
            @Override
            public boolean onQueryTextSubmit(String query) {

                if (!query.isEmpty()) {
                    menu.findItem(R.id.next).setVisible(true);
                    menu.findItem(R.id.prev).setVisible(true);

                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
                        wvDummies.findAllAsync(query);
                    } else {
                        wvDummies.findAll(query);
                    }

                    try {
                        Method m = WebView.class.getMethod("setFindIsUp",
                                Boolean.TYPE);
                        m.invoke(wvDummies, true);
                    } catch (Throwable e) {
                        e.printStackTrace();
                    }
                } else {
                    menu.findItem(R.id.next).setVisible(false);
                    menu.findItem(R.id.prev).setVisible(false);
                }
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if (newText.isEmpty()) {
                    try {
                        Method m = WebView.class.getMethod("setFindIsUp",
                                Boolean.TYPE);
                        m.invoke(wvDummies, false);
                    } catch (Throwable ignored) {
                    }
                }
                return true;
            }
        });

        searchView.setOnCloseListener(new SearchView.OnCloseListener() {
            @Override
            public boolean onClose() {
                Log.d("", "setOnCloseListener called");
                try {
                    Method m = WebView.class.getMethod("setFindIsUp",
                            Boolean.TYPE);
                    m.invoke(wvDummies, false);
                } catch (Throwable ignored) {
                }
                return false;
            }
        });
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId) {
        case android.R.id.home:
            finish();
            break;

        case R.id.next:
            wvDummies.findNext(true);
            break;

        case R.id.prev:
            wvDummies.findNext(false);
            break;
        }
        return super.onOptionsItemSelected(item);
    }
}

2 个答案:

答案 0 :(得分:8)

  

编辑:使用SearchView

我正在使用SearchView,而且在Moto G3 marshmallow 6.0中完美地适合我。

我的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.peacock.myproj.MainActivity">

<WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>

在menu.xml中:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item android:id="@+id/action_search"
    android:title="Search"
    android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
    app:showAsAction="ifRoom|collapseActionView"
    app:actionViewClass="android.support.v7.widget.SearchView" />

</menu>

在我的活动中:

public class MainActivity extends AppCompatActivity {

WebView mWebView;
boolean flag = false;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);

    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
    SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);

    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setSubmitButtonEnabled(true);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            if (!query.isEmpty()) {


                if (!flag) {
                    mWebView.findAllAsync(query);

                    flag = true;
                    try {
                        Method m = WebView.class.getMethod("setFindIsUp", Boolean.TYPE);
                        m.invoke(mWebView, true);
                    } catch (Throwable ignored) {
                    }
                } else {
                    mWebView.findNext(true);
                }
            }
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

            if (newText.isEmpty()) {
                try {
                    Method m = WebView.class.getMethod("setFindIsUp", Boolean.TYPE);
                    m.invoke(mWebView, false);
                } catch (Throwable ignored) {
                }
            }
            flag=false;
            return true;
        }
    });
    searchView.setOnCloseListener(new SearchView.OnCloseListener() {
        @Override
        public boolean onClose() {

            try {
                Method m = WebView.class.getMethod("setFindIsUp", Boolean.TYPE);
                m.invoke(mWebView, false);
            } catch (Throwable ignored) {
            }
            return false;
        }
    });
    return true;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mWebView = (WebView) findViewById(R.id.webview);
    // Load the url
    mWebView.loadUrl("http://stackoverflow.com/questions/38254152/android-webview-findnext-does-not-scroll-to-next-selected-word");

}
}
  

点击提交按钮即可转到下一个单词。见下面的gif:

答案 1 :(得分:2)

我使用searchview可以在单击键盘上搜索时选择单词

public class SOActivity extends AppCompatActivity {

private WebView webview;
private SearchView sv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_so);
    webview = (WebView) findViewById(R.id.webview);
    sv = (SearchView) findViewById(R.id.searcheview);
    initWebView();


}

private void initWebView() {
    webview.loadUrl("http://www.stackoverflow.com");
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

    });

    sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            webview.findAllAsync(query);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{

    if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {

        webview.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="g7.com.example.cgx_pc.cameraapp.SOActivity">


    <android.support.v7.widget.SearchView
        android:id="@+id/searcheview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.v7.widget.SearchView>

<WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</WebView>

enter image description here