AdroidManifest.XML文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/white"
>
<WebView
android:id="@+id/WebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
MainActivity源码:
package com.liyahong.qqmusic.fragment;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import com.ibc.ilanglang.qqmusic.R;
public class Find_fragment extends Fragment {
private WebView webView;
private ImageView stopflush,flush,back;
private String Url = "http://y.qq.com/";
@SuppressLint("SetJavaScriptEnabled")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//加载子布局
View view = View.inflate(getActivity(), R.layout.find_fragment, null);
webView = (WebView) view.findViewById(R.id.WebView);
stopflush = (ImageView) view.findViewById(R.id.stopflush);
flush = (ImageView) view.findViewById(R.id.flush);
back = (ImageView) view.findViewById(R.id.back);
//加载访问地址
webView.loadUrl(Url);
// 支持JavaScript
webView.getSettings().setJavaScriptEnabled(true);
//设置webView支持缩放
webView.getSettings().setBuiltInZoomControls(true);
//支持保存数据
webView.getSettings().setSaveFormData(false);
//覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为,使网页用WebView打开
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
webView.loadUrl(url);
//返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器
return true;
}
});
initListener();
return view;
}
private void initListener(){
//停止页面刷新
stopflush.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
webView.stopLoading();
}
});
//刷新页面
flush.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
webView.reload();
}
});
//返回
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
webView.goBack();
}
});
}
}