帮我实现 Android之实现长按Webview页面文字自定义复制、全选、分享、搜索、翻译功能完整代码
时间: 2024-01-31 15:04:58 浏览: 238
好的,以下是实现长按Webview页面文字自定义复制、全选、分享、搜索、翻译功能的完整代码。
首先,需要在AndroidManifest.xml中添加以下权限:
```xml
<uses-permission android:name="android.permission.INTERNET"/>
```
接着,在布局文件中添加一个WebView:
```xml
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
然后,在Java代码中获取WebView并设置长按监听器:
```java
WebView webView = findViewById(R.id.webview);
registerForContextMenu(webView);
webView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
WebView.HitTestResult result = ((WebView) v).getHitTestResult();
if (result.getType() == WebView.HitTestResult.UNKNOWN_TYPE) {
return false;
}
openContextMenu(v);
return true;
}
});
```
接着,重写onCreateContextMenu方法,自定义菜单:
```java
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
WebView.HitTestResult result = ((WebView) v).getHitTestResult();
if (result.getType() == WebView.HitTestResult.TEXT_TYPE) {
String selectedText = result.getExtra();
// 复制
MenuItem copyItem = menu.add("复制");
copyItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("text", selectedText);
clipboard.setPrimaryClip(clip);
return true;
}
});
// 全选
MenuItem selectAllItem = menu.add("全选");
selectAllItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
webView.selectAll();
return true;
}
});
// 分享
MenuItem shareItem = menu.add("分享");
shareItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, selectedText);
startActivity(Intent.createChooser(intent, "分享"));
return true;
}
});
// 搜索
MenuItem searchItem = menu.add("搜索");
searchItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, selectedText);
startActivity(intent);
return true;
}
});
// 翻译
MenuItem translateItem = menu.add("翻译");
translateItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
String url = "https://translate.google.com/#view=home&op=translate&sl=auto&tl=en&text=" + selectedText;
webView.loadUrl(url);
return true;
}
});
}
}
```
这样,长按WebView页面文字时就会弹出自定义的菜单,包含复制、全选、分享、搜索、翻译功能。
完整代码如下:
```java
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
registerForContextMenu(webView);
webView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
WebView.HitTestResult result = ((WebView) v).getHitTestResult();
if (result.getType() == WebView.HitTestResult.UNKNOWN_TYPE) {
return false;
}
openContextMenu(v);
return true;
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl("https://www.baidu.com");
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
WebView.HitTestResult result = ((WebView) v).getHitTestResult();
if (result.getType() == WebView.HitTestResult.TEXT_TYPE) {
String selectedText = result.getExtra();
// 复制
MenuItem copyItem = menu.add("复制");
copyItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("text", selectedText);
clipboard.setPrimaryClip(clip);
return true;
}
});
// 全选
MenuItem selectAllItem = menu.add("全选");
selectAllItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
webView.selectAll();
return true;
}
});
// 分享
MenuItem shareItem = menu.add("分享");
shareItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, selectedText);
startActivity(Intent.createChooser(intent, "分享"));
return true;
}
});
// 搜索
MenuItem searchItem = menu.add("搜索");
searchItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, selectedText);
startActivity(intent);
return true;
}
});
// 翻译
MenuItem translateItem = menu.add("翻译");
translateItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
String url = "https://translate.google.com/#view=home&op=translate&sl=auto&tl=en&text=" + selectedText;
webView.loadUrl(url);
return true;
}
});
}
}
}
```
阅读全文