开发中一般都会遇到一个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);
设置视频播放