Android中的片段:使用WebView和外部链接

时间:2016-05-08 20:46:20

标签: android android-fragments webview

我创建应用程序并且在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”,则会在默认浏览器中重定向到打开链接。

有人能帮助我吗?

1 个答案:

答案 0 :(得分:1)

尝试移动“公共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();
}

}  运行代码段