admin管理员组

文章数量:1570452

首先简单介绍一下WebView的用法与普通的ImageView组件用法基本相似,他提供了大量的方法来执行浏览器的操作。

WebView的内容可以再当前的组件下面打开也可以在默认浏览器打开,这些都是可控的。。。。。。

下面用代码实际操作一下

WebView布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="vertical" >
	<LinearLayout 
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:orientation="horizontal">
	    <!-- 搜索框 -->
	    <EditText 
		    android:id="@+id/url"
		    android:layout_width="275dp"
		    android:layout_height="60dp"
		    android:layout_marginRight="0px"
		    android:singleLine="true"/>
	    <!-- 确认按钮 -->
	    <Button 
	        android:id="@+id/search"
	        android:layout_width="50dp"
	        android:layout_height="60dp"
	        android:layout_marginRight="0px"
	        android:text="搜索"/>
	</LinearLayout>
	<!-- 定义一个WebView显示网页内容 -->
	<WebView 
	    android:id="@+id/show"
	    android:layout_marginTop="0px"
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent"/>
</LinearLayout>

AndroidMainifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest 
	......
	<uses-permission android:name="android.permission.INTERNET"/>
    <application
		......
        <activity
			......
            android:theme="@android:style/Theme.NoTitleBar"
            ......
        </activity>
    </application>
</manifest>


主程序:

package com.example.minibrowser;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
	EditText url;
	WebView show;
	Button search;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//获取页面中文本框,WebView组件
		url = (EditText) findViewById(R.id.url);
		show = (WebView) findViewById(R.id.show);
		search = (Button) findViewById(R.id.search);
		StringBuilder html = new StringBuilder();
		html.append("<html>");
		html.append("<head>");
		html.append("<title>stven_king欢迎您</title>");
		html.append("</head>");
		html.append("<body>");
		html.append("<h2>北京欢迎您</h2>");
		html.append("</body>");
		html.append("</html>");
		//这种方式可能导致乱码
		//show.loadData(html.toString(), "text/html", "utf-8");
		show.loadDataWithBaseURL(null, html.toString(), "text/html", "utf-8", null);
		//打开本地文件
		//show.loadUrl("file:///android_stven_king/new.html");
		//设置webview的打开方式为在当前的Webview下打开
		show.setWebViewClient(new MyWebViewClient3());
		search.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String urlstr = url.getText().toString();
				System.out.println(urlstr);
				show.loadUrl(urlstr);
			}
		});
	}
	/**
	 * 自定义WebView类,重载shouldOverrideUrlLoading,改变打开方式
	 * 
	 * */
	private class MyWebViewClient extends WebViewClient{
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url){
			view.loadUrl(url);
			return true;
		}
	}
	/**
	 * 自定义WebView类,重载shouldOverrideUrlLoading,改变打开方式
	 * 
	 * */
	private class MyWebViewClient2 extends WebViewClient {
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			// TODO Auto-generated method stub
			super.onPageStarted(view, url, favicon);
		}

	}
	/**
	 * 自定义WebView类,重载shouldOverrideUrlLoading,改变打开方式
	 * 
	 * */
	private class MyWebViewClient3 extends WebViewClient{
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url){
			//遇到特定的连接用其他浏览器打开
			if (url.equals("http://www.baidu")) {
				view.loadUrl(url);
				return false;
			} else {
				Uri uri = Uri.parse(url); // url为你要链接的地址
				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(intent);
			}
			return true;
		}
	}
}


本文标签: 浏览器方式链接androidwebview