我创建了一个WebView
,其中一些单词显示为标签,这些标签必须是可点击的,以便在点击标签后,向用户显示新页面中的相关内容列表。为此,我将这些单词放在WebView
的超链接中。我需要在每个标签中找到文本" a"单击超链接后。但是为webviewclient
定义WebView
工作。
这是我的主要活动:
public class MainActivity extends AppCompatActivity {
String[] wordCloud = new String[]{"Donut", "Eclair", "Froyo", "Gingerbread", "Honeycomb",
"Ice Cream Sandwich", "Jelly Bean", "KitKat", "Lollipop", "Marshmallow"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView d3 = (WebView) findViewById(R.id.d3);
WebSettings ws = d3.getSettings();
ws.setJavaScriptEnabled(true);
String span = "";
String html = "<HTML><head>\n" +
" <meta charset=\"utf-8\">\n" +
" <meta name=\"viewport\" content=\"width=device-width, user-scalable=no\">\n" +
"</head>\n" +
"<body>\n";
for (int i = 0; i < wordCloud.length; i++) {
span = span + "<span data-weight=\"" + 5 + "\" >" + wordCloud[i] + "</span><a href=\"example.com/tags/"+wordCloud[i] +"\">"+ wordCloud[i] +"</a><br/>\n";
}
html= html +span +
"<span data-weight=\"6\" > some example </span><br/>\n" +
"</body></HTML>";
d3.loadData(html, "text/html; charset=utf-8", "UTF-8");
d3.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String[] parts = url.split("/");
String tag=parts[2];
Toast.makeText(getApplicationContext(),tag, Toast.LENGTH_LONG).show();
return true;
}
});
}
}