当前位置: 编程技术>移动开发
android webview 简单浏览器实现代码
来源: 互联网 发布时间:2014-10-17
本文导语: 文件main.java 代码如下: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.w...
文件main.java
代码如下:
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(/tech-mobile-dev/wv,strurl/index.html);
}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(/tech-mobile-dev/view,url/index.html);
return true;
}
});
}
public void loadurl(/tech-mobile-dev/final WebView view,final String url/index.html){
new Thread(){
public void run(){
view.loadUrl(/tech-mobile-dev/url/index.html);
}
}.start();
}
}
2布局文件main.xml
代码如下: