android webview 简单浏览器实现代码
package com.HHBrowser.android;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
public class main extends Activity {
/** Called when the activity is first created. */
WebView wv;
Handler handler;
Button btnButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wv = (WebView)findViewById(R.id.webView1);
wv.getSettings().setJavaScriptEnabled(true);
wv.setScrollBarStyle(0);
wv.loadUrl("http://www.baidu.com");
btnButton = (Button)findViewById(R.id.turn);
btnButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText editText = (EditText)findViewById(R.id.editText1);
String strurl = String.valueOf(editText.getText());
if(strurl.contains("http://"))
{
Log.v("ttt", strurl);
loadurl(wv,strurl);
}else {
Log.v("ttt", strurl);
loadurl(wv,"http://www.baidu.com");
}
}
});
wv.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
EditText newText = (EditText)findViewById(R.id.editText1);
newText.setText(url);
loadurl(view,url);
return true;
}
});
}
public void loadurl(final WebView view,final String url){
new Thread(){
public void run(){
view.loadUrl(url);
}
}.start();
}
}
<http://schemas.android.com/apk/res/Android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android:layout_height="wrap_content"
android:baselineAligned="false">
<android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText1"
android:maxEms="15"
android:minEms="10"
android:layout_gravity="center_vertical"
>
android:id="@+id/turn"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
代码如下:
<android:layout_height="wrap_content"
android:baselineAligned="false">
<android:layout_width="fill_parent"
android:layout_height="wrap_content">
配置文件AndroidManifest.xml里面需要设置