我有一个webview,从webservice中调用其中的数据,在webview中,整个描述中有一个链接。我的问题是,我只想在单击该链接时打开一个新活动,而不是单击webview或触摸webview。
我有一个webview,从webservice中调用其中的数据,在webview中,整个描述中有一个链接。我的问题是,我只想在单击该链接时打开一个新活动,而不是单击webview或触摸webview。
你需要为shouldOverrideUrlLoading
提供实现。你将需要为你的webview设置一个WebViewClient
,在这个方法中,你需要编写一些逻辑来识别这个链接并打开新的Activity
。例如:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wv = (WebView) findViewById(R.id.myWebView);
wv.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(isURLMatching(url)) {
openNextActivity();
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
});
}
protected boolean isURLMatching(String url) {
// some logic to match the URL would be safe to have here
return true;
}
protected void openNextActivity() {
Intent intent = new Intent(this, MyNextActivity.class);
startActivity(intent);
}