gpt4 book ai didi

android - Android : using WebView with external links 中的 fragment

转载 作者:行者123 更新时间:2023-11-30 01:21:01 24 4
gpt4 key购买 nike

我创建了应用程序,但在 Fragment 中使用带有外部链接的 WebView 时遇到问题。

我有 webView_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="by.beep.materialvebweiv.activity.FriendsFragment">

<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/webView" />

</RelativeLayout>

和类

package by.beep.materialvebweiv.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import by.beep.materialvebweiv.R;

public class FriendsFragment extends Fragment {

public FriendsFragment() {
}

public WebView mWebView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_friends, container, false);
mWebView = (WebView) v.findViewById(R.id.webView);
mWebView.loadUrl("https://google.com");

WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

mWebView.setWebViewClient(new WebViewClient());

return v;
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}

@Override
public void onDetach() {
super.onDetach();
}
}

我尝试在下一个案例中使用 Fragment 中的 WebView:如果 URL 不包含“google.com”重定向以在默认浏览器中打开链接。

谁能帮帮我?

最佳答案

尝试将 public WebView mWebView; 移动到 public FriendsFragment(){}

之上

这将为您提供以下代码:

public class FriendsFragment extends Fragment {

public WebView mWebView;
public ProgressBar progressBar;
public FrameLayout frameLayout;
public SwipeRefreshLayout swipeRefreshLayout;

public FriendsFragment() {
}


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_friends, container, false);
mWebView = (WebView) v.findViewById(R.id.webView);
mWebView.loadUrl("https://google.com");

WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

mWebView.setWebViewClient(new WebViewClient());

return v;
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}

@Override
public void onDetach() {
super.onDetach();
}
}

关于android - Android : using WebView with external links 中的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37104640/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com