gpt4 book ai didi

android - 如何在 WebView 文本选择菜单中保留标准的 'select all' 和 'copy' 菜单项?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:14:39 26 4
gpt4 key购买 nike

我需要使用自定义命令扩展 android WebView 的文本选择菜单。到目前为止,我已经设法通过以下方式实现了我的自定义 webview 文本选择菜单:

public class CustomActionWebView extends WebView {

static String TAG = "CustomActionWebView";

ActionMode mActionMode;

int mMenuID;
Activity mParentAct;

ActionSelectListener mActionSelectListener;
ButtonClickListener mButtonClick;

public CustomActionWebView(Context context) {
super(context);
}

public CustomActionWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public CustomActionWebView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

public void setActionMenu(int mActionMenu, Activity ParentAct) {

setWebViewClient(new CustomWebViewClient());
this.mMenuID = mActionMenu;
mParentAct = ParentAct;
}

private ActionMode resolveActionMode(ActionMode actionMode) {
if (actionMode != null) {
final Menu menu = actionMode.getMenu();
mActionMode = actionMode;
menu.clear();

mParentAct.getMenuInflater().inflate(mMenuID, menu);

for (int i = 0; i < menu.size(); i++) {
MenuItem mi = menu.getItem(i);

mi.setOnMenuItemClickListener(item -> {
int id=item.getItemId();
if(id == R.id.cab_select_all)
{
String js = "var el =document.getElementsByTagName(\"BODY\")[0]; el.setSelectionRange(0, el.value.length); alert(el.value);)";
evaluateJavascript("javascript:" + js, null);
}
else getSelectedData( item.getItemId());
releaseAction();
return true;
});

}

}
mActionMode = actionMode;
return actionMode;
}

@Override
public ActionMode startActionMode(ActionMode.Callback callback) {
ActionMode actionMode = super.startActionMode(callback);
return resolveActionMode(actionMode);
}

@Override
public ActionMode startActionMode(ActionMode.Callback callback, int type) {
ActionMode actionMode = super.startActionMode(callback, type);
return resolveActionMode(actionMode);
}

private void releaseAction() {
if (mActionMode != null) {
mActionMode.finish();
mActionMode = null;
}
}

private void getSelectedData(int menuID) {


String js2 = "(function getSelectedText() {" +
"var txt;" +
"var parentID;" +
"var test;" +
"var menuID = " + menuID + ";" +
"if (window.getSelection) {" +
"txt = window.getSelection().toString();" +
"parentID = window.getSelection().baseNode.parentNode.id;"+
"test = '1st';"+
"} else if (window.document.getSelection) {" +
"txt = window.document.getSelection().toString();" +
"parentID = window.document.getSelection().baseNode.parentNode.id;"+
"test = '2nd';"+
"} else if (window.document.selection) {" +
"txt = window.document.selection.createRange().text;" +
"parentID = window.document.selection.baseNode.parentNode.id;"+
"test = '3rd';"+
"}" +
"JSInterface.callback(txt, menuID, parentID);" +
"})()";


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
evaluateJavascript("javascript:" + js2, null);
} else {
loadUrl("javascript:" + js2);
}
}

public void linkJSInterface() {
addJavascriptInterface(new ActionSelectInterface(this), "JSInterface");
addJavascriptInterface(new ButtonClickInterface(this), "ButtonClick");
}

public void setActionSelectListener(ActionSelectListener actionSelectListener) {
this.mActionSelectListener = actionSelectListener;
}

public void setVerseToolbarClickListener(ButtonClickListener mButtonClick) {
this.mButtonClick = mButtonClick;
}

public void dismissAction() {
releaseAction();
}


private class ActionSelectInterface {

CustomActionWebView mContext;

ActionSelectInterface(CustomActionWebView c) {
mContext = c;
}

@JavascriptInterface
public void callback(final String value, final int menuID, final String parentDivID) {
if (mActionSelectListener != null) {
mActionSelectListener.onTextSelectionMenuClick(menuID, value, parentDivID);
}
}

@JavascriptInterface
public void hl_click(long row_id) {
if (mActionSelectListener != null) {
mActionSelectListener.onHighlightClick(row_id);
}
}
}

private class ButtonClickInterface {

CustomActionWebView mContext;

ButtonClickInterface(CustomActionWebView c) {
mContext = c;
}

@JavascriptInterface
public void call(final String id) {
if (mButtonClick != null) {
mButtonClick.onClick(id);
}
}
}


private class CustomWebViewClient extends WebViewClient {

private boolean mLastLoadFailed = false;

@Override
public void onPageFinished(WebView webView, String url) {
super.onPageFinished(webView, url);
if (!mLastLoadFailed) {
CustomActionWebView customActionWebView = (CustomActionWebView) webView;
customActionWebView.linkJSInterface();

}
}

@Override
public void onPageStarted(WebView webView, String url, Bitmap favicon) {
super.onPageStarted(webView, url, favicon);
}

@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
mLastLoadFailed = true;
}
}


}

然而,这种实现导致缺少标准的 webview“复制”和“全选”菜单项。我可以实现我自己的 COPY 菜单项,但是我不知道如何拥有标准的“SELECT ALL”菜单项的等价物或以这种方式实现我自己的自定义 webview 菜单以使其保留标准菜单。谁能建议我如何让“SELECT ALL”菜单项正常工作?

最佳答案

我绝对不鼓励您在需要使用旧菜单中的功能时创建新的选择菜单。

关键:你只需要修改当前的

是的,我们可以通过添加新的选择标签或删除旧的选择标签来编辑当前的选择菜单。

代码解释说:

@Override
public ActionMode startActionMode(ActionMode.Callback callback, int type) {
// Get the original action menu
ActionMode modifiedMode = super.startActionMode(callback, type);

// Obtain the menu reference
Menu menu = modifiedMode.getMenu();

// add new selection with click listener
menu.add("test").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
Toast.makeText(getContext(),"مرحباً", Toast.LENGTH_LONG).show();
return true;
}
});

// remove the 'cut' selection from menu
menu.removeItem(android.R.id.cut);

return modifiedMode;
}

关于android - 如何在 WebView 文本选择菜单中保留标准的 'select all' 和 'copy' 菜单项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56788779/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com