android -- 一个简单的浏览器

本文介绍如何在Android应用中使用Webview控件创建一个简单的自定义浏览器,包括设置网页大小自适应、支持缩放、启用JavaScript等功能,并通过代码示例展示了基本的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开发中一般都会遇到一个webview 的控件。也就是网页控件,通过一些简单的设置和操作,能够进行一个简单的自定义的浏览器。

首先先看一下效果。


                                                      

     正常显示效果              


      

放大后效果

                                        

                                                                                                                                         


当输入网址后,就会自动跳转,按返回键后,如果能返回,页面将会返回,但没有做提示。这里只是做一个简单的实例,希望对大家有帮助。


代码:

package com.myweb.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MyWebActivity extends Activity {
	private WebView wv_myweb;
	private EditText tv_address;
	private Button bt_go;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		this.wv_myweb = (WebView) this.findViewById(R.id.wv_myweb);
		this.tv_address = (EditText) this.findViewById(R.id.tv_address);
		this.bt_go = (Button) this.findViewById(R.id.bt_go);
		
		
		this.wv_myweb.getSettings().setUseWideViewPort(true); //setting the web size is self-adapt
		this.wv_myweb.getSettings().setLoadWithOverviewMode(true);
		
		this.wv_myweb.getSettings().setBuiltInZoomControls(true);//setting the webview can zoom
		
		
		this.wv_myweb.getSettings().setJavaScriptEnabled(true);//support javascript
		
		this.wv_myweb.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		this.wv_myweb.getSettings().setPluginsEnabled(true); 
		this.wv_myweb.getSettings().setPluginState(PluginState.ON); 
		
		this.wv_myweb.setWebViewClient(new WebViewClient(){//setting the show the web on itself, when click a link
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
		});
		
		
		this.bt_go.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				wv_myweb.loadUrl("http://"+tv_address.getText().toString().trim());
			}
		});
		
	}
	
	@Override
	public void onBackPressed() {//the web page go back
		// TODO Auto-generated method stub
//		suyuper.onBackPressed();
		
		if(this.wv_myweb.canGoBack()){
			this.wv_myweb.goBack();
		}else{
			Toast.makeText(getApplicationContext(),"this is the last page!", 0).show();
		}
	}
}

布局文件

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:layout_margin="5dip" >

        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.2" >

            <EditText
                android:id="@+id/tv_address"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:hint="type in the address"
                android:singleLine="true" />
        </ScrollView>

        <Button
            android:id="@+id/bt_go"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.8"
            android:background="@drawable/selector_bt_go_back"
            android:text="Go" />
    </LinearLayout>

    <WebView
        android:id="@+id/wv_myweb"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>


其中一些属性的说明:
this.wv_myweb.setWebViewClient(new WebViewClient(){//setting the show the web on itself, when click a link
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});


如果不设置这个,那么点击链接的时候,将会有系统自带浏览器,或者其他浏览器打开。
                
this.wv_myweb.getSettings().setUseWideViewPort(true); //setting the web size is self-adapt
this.wv_myweb.getSettings().setLoadWithOverviewMode(true);
上方两行是设置页面大小自适应屏幕的,如果不设置,那么打开网页的时候,很多网站将会溢出屏幕。   


this.wv_myweb.getSettings().setBuiltInZoomControls(true);//setting the webview can zoom
设置缩放页面,如果不设置,将不能缩放网站。




this.wv_myweb.getSettings().setJavaScriptEnabled(true);//support javascript
设置支持javascript。


this.wv_myweb.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
this.wv_myweb.getSettings().setPluginsEnabled(true); 
this.wv_myweb.getSettings().setPluginState(PluginState.ON); 
设置视频播放






                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值