Getting selected text in a WebView via a contextual action bar

做~自己de王妃 提交于 2019-12-02 18:39:10

You can't do that yet with the current API.

I filed a feature request for this - Issue 24841: WebView should allow applications to supply a custom Contextual Action Bar http://code.google.com/p/android/issues/detail?id=24841

Basically, WebView in 4.0 has hardcoded its own Contextual Action Bar (CAB). That CAB has a reference back to the WebView and with that reference, it can get the selected text. I'm not sure how you were able to detect the ActionMode starting and modify the menu, but if you were able to do all of that, then you are stuck because getSelection() is package-private currently. I filed that as a separate issue and linked it to the previous issue above.

You can use javascript to get the selected text: window.getSelection(), and use WebView's addJavascriptInterface function to return the result.

thanks for your information, I have solved a hard issue.. I just want to add some function into the actionmode. The following is my code, May be helpful to others.

@Override
public ActionMode onWindowStartingActionMode(Callback callback) {
    // TODO Auto-generated method stub
    ActionMode mode = super.onWindowStartingActionMode(callback);
    mode.getMenuInflater().inflate(R.menu.actions, mode.getMenu());
    mode.getMenu().findItem(R.id.action_add).setOnMenuItemClickListener(new OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            // TODO Auto-generated method stub
            Log.i("", "onMenuItemClick add ");
            return false;
        }
    });
    return mode;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!