android - Android Studio:单击网页视图中的超链接后如何打开新的活动?
问题描述
这些天我为我的学校构建了一个应用程序,一个抽屉片段包含一个带有学校新闻的 web 视图。文章下方的按钮可在单击它们后打开包含完整文章的新链接。(如“阅读更多” - 按钮)。
我想在单击它们后打开一个新活动,与单击哪篇文章无关,因为我不需要为每篇新闻文章创建一个活动。
我希望你能理解我的问题并给我一个解决方案。谢谢
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="@android:color/white">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</RelativeLayout>
static WebView webview;
private View mContentView;
SwipeRefreshLayout swipe;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message message) {
switch (message.what) {
case 1:{
webViewGoBack();
}break;
}
}
};
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_news, container, false);
swipe = (SwipeRefreshLayout) v.findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
loadWeb(v);
}
});
loadWeb(v);
return v;
}
private void webViewGoBack(){
webview.goBack();
}
public void loadWeb(final View v) {
webview = (WebView) v.findViewById(R.id.webView);
WebSettings webSettings = webview.getSettings();
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webview.getSettings().setAppCacheEnabled(true);
webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY );
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setEnableSmoothTransition(true);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview.setLayerType(WebView.LAYER_TYPE_NONE, null);
swipe.setRefreshing(true);
webview.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
@Override
public void onPageFinished(WebView view, String url) {
swipe.setRefreshing(false);
{
webview.loadUrl
("javascript:(function() { " +
"document.getElementsByTagName('header')[0].style.display='none'; " +
"document.getElementsByTagName('nav')[0].style.display='none'; " +
"document.getElementsByTagName('footer')[0].style.display='none'; " +
"document.getElementsByClassName('widgettitle')[0].remove(); " +
"document.getElementsByClassName('widgettitle')[0].remove(); " +
"document.getElementsByClassName('widgettitle')[0].remove(); " +
"document.getElementsByClassName('widget widget_search')[0].remove(); " +
"document.getElementsByClassName('widget widget_recent_entries')[0].remove(); " +
"document.getElementsByClassName('widget widget_archive')[0].remove(); " +
"document.getElementsByClassName('cat-item cat-item-3')[0].remove(); " +
"document.getElementsByClassName('l-header pos_fixed bg_solid shadow_wide')[0].remove(); " +
"document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit post_taxonomy style_simple')[0].remove(); " +
"document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit post_taxonomy style_simple')[0].remove(); " +
"document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit post_taxonomy style_simple')[0].remove(); " +
"document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit post_taxonomy style_simple')[0].remove(); " +
"document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit post_taxonomy style_simple')[0].remove(); " +
"document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit post_taxonomy style_simple')[0].remove(); " +
"document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit post_taxonomy style_simple')[0].remove(); " +
"document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit post_taxonomy style_simple')[0].remove(); " +
"document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit post_taxonomy style_simple')[0].remove(); " +
"document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit post_taxonomy style_simple')[0].remove(); " +
"document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit post_author vcard author')[0].remove(); " +
"document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit post_author vcard author')[0].remove(); " +
"document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit post_author vcard author')[0].remove(); " +
"document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit post_author vcard author')[0].remove(); " +
"document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit post_author vcard author')[0].remove(); " +
"document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit post_author vcard author')[0].remove(); " +
"document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit post_author vcard author')[0].remove(); " +
"document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit post_author vcard author')[0].remove(); " +
"document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit post_author vcard author')[0].remove(); " +
"document.getElementsByClassName('grecaptcha-badge')[0].remove(); " +
"document.getElementsByClassName('rc-anchor-logo-img rc-anchor-logo-img-large')[0].remove(); " +
"})()");
webview.setVisibility(View.VISIBLE);
}
}
});
webview.loadUrl("https://www.bbs-varel.de/aktuelles/");
webview.setOnKeyListener(new View.OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == MotionEvent.ACTION_UP
&& webview.canGoBack()) {
handler.sendEmptyMessage(1);
return true;
}
return false;
}
});
}
}
解决方案
有一个办法就是。简而言之,它包括向 webview 注册一个侦听器,以便首先将每个页面导航(即单击按钮时加载的新页面)传递给您。
去做这个:
Webview webview = findViewById(R.id.webView)
webView.setWebViewClient(webViewClient);
webView.setWebChromeClient(new WebChromeClient());
你的班级在哪里webViewClient
实施android.webkit.WebViewClient
. 在那里,您会找到一种方法android.webkit.WebViewClient#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String)
,它可以让您“挂钩”来检查用户是否单击了您感兴趣的内容。如果返回 true,则不会加载新页面。如果您返回 false,则页面的行为就像没有附加侦听器一样。祝你好运!
推荐阅读
- javascript - 循环数:将一个数字除以另一个数字时如何获得一个非四舍五入的循环数?
- python - 正则表达式仅在成对括号后对字符进行正向前瞻
- java - 如何安排任务以便它在android中的任何条件下执行
- reactjs - mdbreact 表过滤器无法使用 React
- ios - 以编程方式将渐变视图和 UILabel 添加到 UIView 不起作用
- ionic-framework - 为什么 Ionic contacts.find 会冻结我的应用程序?
- javascript - 如何在 JavaScript Uint8Array 中存储字符代码?
- c# - 如果满足条件,则跳过序列化列表元素 (Newtonsoft)
- amazon-web-services - 如何使用 AWS Glue 将位于本地的文件加载到 AWS
- c++ - 如何通过 C++ API 向 tflite 提供多维输入