如何从android中的webview获取所选部分的开始和结束位置

发布于 2024-12-22 09:45:14 字数 88 浏览 2 评论 0原文

在网络视图中,我发现长按时默认选择。但是选择后,我们必须单击所选部分才能复制到剪贴板。我想在按下按钮时将所选部分复制到剪贴板。我怎样才能做到这一点,请帮助我...

In web view i found default selection on long press.but after selection we have to click on the selected portion to copy to clipboard. i want to copy the selected portion to clipboard when a button presses.how can i make it possible please help me...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呢古 2024-12-29 09:45:14

您可以执行 webview 的方法 javascript:

方法 javascript 获取元素 id、开始位置和结束位置

 function(){
 var html = ""; 
 if (typeof window.getSelection != "undefined") {
  var sel = window.getSelection(); 
 } 
 return      sel.anchorNode.parentElement.id+':'+sel.anchorOffset+':'+sel.focusOffset; 
 })();

在 webview 上执行:

webview.evaluateJavascript(script, new ValueCallback<String>() {
    @Override
    public void onReceiveValue(String s) {
        selectedData = s; //value of javascript return
        selectedData = selectedData.replaceAll("\"", "");
        String array[] = new String[3];
        elementId = array[0];
        startPosition=array[1];
        endPosition=array[2];
        Log.d("WebView runtime", selectedText); 
    }
});

如果您想获取选择的文本,请使用此:

 function(){
 var html = ""; 
 if (typeof window.getSelection != "undefined") {
  var sel = window.getSelection(); 
 } 
 return sel.text; 
 })();

要执行此方法的调用,

private class TesteCallback implements Callback {

    private MyWebView mywebview;

    private TextMark textmark;

    public TesteCallback(MyWebView mywebview) {
        this.mywebview = mywebview;

    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {

        mode.setTitle("Chose color");

        mode.getMenuInflater().inflate(R.menu.pagina2, menu);

        return true;
    }

    /**
     * create custom itens, remove useless itens
     */
    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        menu.removeItem(android.R.id.selectAll);
        // Remove the "cut" option
        menu.removeItem(android.R.id.cut);
        // Remove the "copy all" option
        menu.removeItem(android.R.id.copy);

        return true;
    }
@Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        mywebview.pegarSelecao();

        textmark = new TextMark();
        switch (item.getItemId()) {
        case R.id.red:
            // do some stuff

            break;
        case R.id.yellow:
            // do some stuff

            break;
        case R.id.blue:
            // do some stuff

            break;
        default:

            break;
        }

        return false;
    }
}//end class

如果需要使用, 您可以创建自定义回调自定义回调您需要创建自定义 webview 并调用回调:

public class MyWebView extends WebView {
public MyWebView(Context context) {

    super(context);
    this.contexto = context;

}
public MyWebView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

public MyWebView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}
private ActionMode.Callback mActionModeCallback;

///////////////////////////////////////////
@Override
public ActionMode startActionMode(Callback callback) {
    ViewParent parent = getParent();
    if (parent == null) {
        return null;
    }

    mActionModeCallback = new TesteCallback(this); //this line call custom callback
    return parent.startActionModeForChild(this, mActionModeCallback);
}

}

You can execute method javascript of webview:

Method javascript to get element id, start position and end position

 function(){
 var html = ""; 
 if (typeof window.getSelection != "undefined") {
  var sel = window.getSelection(); 
 } 
 return      sel.anchorNode.parentElement.id+':'+sel.anchorOffset+':'+sel.focusOffset; 
 })();

executing on webview:

webview.evaluateJavascript(script, new ValueCallback<String>() {
    @Override
    public void onReceiveValue(String s) {
        selectedData = s; //value of javascript return
        selectedData = selectedData.replaceAll("\"", "");
        String array[] = new String[3];
        elementId = array[0];
        startPosition=array[1];
        endPosition=array[2];
        Log.d("WebView runtime", selectedText); 
    }
});

if you want get text of selection use this:

 function(){
 var html = ""; 
 if (typeof window.getSelection != "undefined") {
  var sel = window.getSelection(); 
 } 
 return sel.text; 
 })();

to execute calling of this method you can create a custom callback

private class TesteCallback implements Callback {

    private MyWebView mywebview;

    private TextMark textmark;

    public TesteCallback(MyWebView mywebview) {
        this.mywebview = mywebview;

    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {

        mode.setTitle("Chose color");

        mode.getMenuInflater().inflate(R.menu.pagina2, menu);

        return true;
    }

    /**
     * create custom itens, remove useless itens
     */
    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        menu.removeItem(android.R.id.selectAll);
        // Remove the "cut" option
        menu.removeItem(android.R.id.cut);
        // Remove the "copy all" option
        menu.removeItem(android.R.id.copy);

        return true;
    }
@Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        mywebview.pegarSelecao();

        textmark = new TextMark();
        switch (item.getItemId()) {
        case R.id.red:
            // do some stuff

            break;
        case R.id.yellow:
            // do some stuff

            break;
        case R.id.blue:
            // do some stuff

            break;
        default:

            break;
        }

        return false;
    }
}//end class

if you need use a custom callback you need create a custom webview and calling callback:

public class MyWebView extends WebView {
public MyWebView(Context context) {

    super(context);
    this.contexto = context;

}
public MyWebView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

public MyWebView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}
private ActionMode.Callback mActionModeCallback;

///////////////////////////////////////////
@Override
public ActionMode startActionMode(Callback callback) {
    ViewParent parent = getParent();
    if (parent == null) {
        return null;
    }

    mActionModeCallback = new TesteCallback(this); //this line call custom callback
    return parent.startActionModeForChild(this, mActionModeCallback);
}

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文