Web ビューでは、長押しでデフォルトの選択が見つかりましたが、選択後、選択した部分をクリックしてクリップボードにコピーする必要があります。ボタンが押されたときに選択した部分をクリップボードにコピーしたいのですが、どうすればそれを可能にすることができますか...
1 に答える
4
webview のメソッド javascript を実行できます。
要素ID、開始位置、終了位置を取得するJavaScriptメソッド
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);
}
}
于 2015-04-29T14:25:32.837 に答える