码迷,mamicode.com
首页 > 移动开发 > 详细

第九章,WebView点击网页内链接跳转到其他Activity(Android)

时间:2015-06-16 19:24:03      阅读:15580      评论:0      收藏:0      [点我收藏+]

标签:android   webview   webviewclient   shouldoverrideurlloa   

在 AndroidManifest.xml中添加网络权限

<uses-permission android:name="android.permission.INTERNET"/>
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"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/main_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="start" />

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

</LinearLayout>
MainActivity.java

package com.example.webviewdemo01;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

	private Button main_button;
	private WebView web_view;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 初始化数据
		initView();

	}

	private void initView() {
		// TODO Auto-generated method stub

		main_button = (Button) this.findViewById(R.id.main_button);
		web_view = (WebView) this.findViewById(R.id.main_web);

		// 设置监听
		main_button.setOnClickListener(this);

	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

		web_view.loadUrl("http://www.baidu.com");

		// WebChromeClienty用来处理WebView的Javascript的对话框、
		// 网站图标、网站title、加载进度等,重写里面的方法即可
		web_view.setWebChromeClient(new WebChromeClient());// 此方法此处可不写

		// WebViewClient用来处理WebView各种通知、请求事件等,重写里面的方法即可
		web_view.setWebViewClient(new WebViewClient() {

			// 点击页面中的链接会调用这个方法
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// TODO Auto-generated method stub

				// 跳转到另外的activity
				Intent intent = new Intent(getApplication(),
						SecondActivity.class);
				startActivity(intent);

				Log.i("qing", "shouldOverrideUrlLoading..." + url);
				return super.shouldOverrideUrlLoading(view, url);
			}

		});

	}

}
运行截图:

技术分享

技术分享

点击上面任何一个链接

技术分享

注:第二个activity的代码和布局因为没写太多内容,这里就没有贴出。

第九章,WebView点击网页内链接跳转到其他Activity(Android)

标签:android   webview   webviewclient   shouldoverrideurlloa   

原文地址:http://blog.csdn.net/qingbowen/article/details/46520273

(3)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!