我创建了一个可以将文本复制到剪贴板的自定义菜单,但是如果HTML页面中有焦点文本字段,我该如何将其粘贴回来?
private class CustomMenuItemClickListener implements MenuItem.OnMenuItemClickListener {
@Override
public boolean onMenuItemClick(MenuItem item) {
final ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
switch (item.getItemId()) {
case R.id.copy:
webView.evaluateJavascript("(function(){return window.getSelection().toString()})()",
new ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {
ClipData clip = ClipData.newPlainText("webView", value);
clipboard.setPrimaryClip(clip);
}
});
break;
case R.id.paste:
ClipData clipData = clipboard.getPrimaryClip();
ClipData.Item item1 = clipData.getItemAt(0);
String text = item1.getText().toString();
//Paste, how to access the selected field in the page?
break;
default:
break;
}
return false;
}
}
答案 0 :(得分:1)
使用document.activeElement.value
,这应该得到重点文本字段
webView.evaluateJavascript("(function(){document.activeElement.value = '"+text+"'})()",
new ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {
}
});