用户界面View之WebView

寒冷到了极致时,太阳就要光临。


本讲内容:WebView显示网页控件


一、通过Intent调用系统浏览器

Uri uri=Uri.parse(url);//url为你要链接的地址
Intent intent=new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);


二、通过WebView显示网页

当用户点击一个WebView中的页面的链接时,默认是在浏览器中打开并加载目标URL的,然而,你可以在WebView中覆盖这一行为,那么链接就会在WebView中打开。

webView.setWebViewClient(new WebViewClient()


三、在WebView中加载页面,使用loadUrl()
web资源:webView.loadUrl("http://www.baidu.com");
本地文件用:webView.loadUrl("file:///android_asset/xx.html");
本地文件存放在:assets文件中

使页面获得焦点
webView.requestFocus();


四、前进与后退
当你的WebView覆盖了URL加载,它会自动生成历史访问记录。可以通过goBack()或goForward()向前或向后访问已访问过的站点。


示例一:


下面是res/layout/activity_main.xml 布局文件:

<LinearLayout 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">
    
    <WebView 
        android:id="@+id/id_webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>


下面是MainActivity主界面文件:

public class MainActivity extends Activity {
	private String url="http://www.baidu.com";
	private WebView webView;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initViews();
	}

	/**
	 * 初始化控件
	 */
	private void initViews() {
		webView=(WebView) findViewById(R.id.id_webView);
		webView.loadUrl(url);
		//启用支持JavaScript
		WebSettings settings = webView.getSettings();
		settings.setJavaScriptEnabled(true);
		
		// 覆盖WebView默认通过第三方或者是系统浏览器打开网页的行为,使得网页可以在WebVIew中打开
		webView.setWebViewClient(new WebViewClient(){
			WebViewClient帮助WebView去处理一些页面控制和请求通知
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				webView.loadUrl(url);
				//true:使网页在WebView中打开,false:调用系统浏览器或第三方浏览器打开
				return true;
			}
		});
	}	
}



示例二:


下面是res/layout/top.xml 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/id_top"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="#ffe85628" >

        <Button
            android:id="@+id/id_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:background="@drawable/selector_button"
            android:text="返回"
            android:textColor="#fff"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/id_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerInParent="true"
            android:textColor="#fff"
            android:textSize="18sp" />

        <Button
            android:id="@+id/id_refresh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:background="@drawable/selector_button"
            android:text="刷新"
            android:textColor="#fff"
            android:textSize="18sp" />
    </RelativeLayout>

</LinearLayout>


下面是res/layout/activity_main.xml 布局文件:

<LinearLayout 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">
    
    <include layout="@layout/top"/>

    <WebView 
        android:id="@+id/id_web"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>


下面是res/drawable/selector_button.xml 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/bg_logout_pressed" android:state_pressed="true"></item>
    <item android:drawable="@drawable/bg_logout_normal"></item>
</selector>

下面是MainActivity.java主界面文件:

public class MainActivity extends Activity implements OnClickListener{
	private Button back;
	private Button refresh;
	private TextView tv;
	
	private WebView webView;
	private ProgressDialog dialog;
	private String url="http://www.baidu.com";
	

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initViews();
	}
	
	/**
	 * 初始化控件
	 */
	private void initViews(){
		tv=(TextView) findViewById(R.id.id_tv);
		back=(Button) findViewById(R.id.id_back);
		refresh=(Button) findViewById(R.id.id_refresh);
		back.setOnClickListener(this);
		refresh.setOnClickListener(this);
		
		webView=(WebView) findViewById(R.id.id_webView);
		webView.loadUrl(url);
		// 启用支持JavaScript
		WebSettings settings = webView.getSettings();
		settings.setJavaScriptEnabled(true);
		//WebView加载页面优先使用缓存加载(默认不使用)提高加载速度
		settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
		
		//WebViewClient主要帮助WebView处理各种通知、请求事件的
		webView.setWebViewClient(new WebViewClient(){
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
		});
		
		//WebChromeClient主要辅助WebView处理Javascript的对话框、网站图标、网站title、加载进度等
		webView.setWebChromeClient(new WebChromeClient(){
			/**
			 * 处理加载进度
			 */
			public void onProgressChanged(WebView view, int newProgress) {
				//newProgress 1-100之间的整数
				if(newProgress==100){
					//网页加载完毕,关闭ProgressDialog
					closeDialog();
				}else{
					//网页正在加载,打开ProgressDialog
					openDialog(newProgress);
				}
			}
			
			/**
			 * 处理网站title
			 */
			public void onReceivedTitle(WebView view, String title) {
				tv.setText(title);
				super.onReceivedTitle(view, title);
			}

			/**
			 * 打开对话框
			 */
			private void openDialog(int newProgress) {
				if(dialog==null){
					dialog=new ProgressDialog(MainActivity.this);
					dialog.setTitle("正在加载");
					dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
					dialog.setProgress(newProgress);
					dialog.show();
				}else{
					dialog.setProgress(newProgress);
				}
			}

			/**
			 * 关闭对话框
			 */
			private void closeDialog() {
				if(dialog!=null&&dialog.isShowing()){
					dialog.dismiss();
					dialog=null;
				}
			}
		});
	}

	/**
	 * 按钮点击事件
	 */
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.id_back:
			if(webView.canGoBack()){
				webView.goBack();//返回上一页面
			}else{
				System.exit(0);//退出程序
			}
			break;
			
		case R.id.id_refresh:
			webView.reload();
			break;
		}
	}
	
	/**
	 * 改写物理按键——返回的逻辑
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK){
			if(webView.canGoBack()){
				webView.goBack();//返回上一页面
				return true;//快速退出
			}else{
				System.exit(0);//退出程序
			}
		}
		return super.onKeyDown(keyCode, event);
	}
	
}




Take your time and enjoy it 要原码的、路过的、学习过的请留个言,顶个呗~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值