可以在界面上配置一个EditText,一个Button,EditText用于输入网址,Button用于访问该网址的动作,在Button的onClickListener事件中编写处理代码
下面是一个界面效果和示例代码:
public class WebActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.weblayout);
final WebView web=(WebView)findViewById(R.id.webView1);
ProgressBar progress=(ProgressBar)findViewById(R.id.progressBar1);
final EditText ed=(EditText)findViewById(R.id.editText1);
Button btn=(Button)findViewById(R.id.button1);
String url="http://www.baidu.com";
initwebView(web,url,progress);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(!ed.getText().toString().equals("")){
web.loadUrl("http://"+ed.getText().toString());
}
}
});
}
public void initwebView(final WebView webView,final String URL,final ProgressBar progressBar){
webView.getSettings().setJavaScriptEnabled(true);//允许展示js脚本
webView.getSettings().setDefaultTextEncodingName("GB2312");//默认编码格式
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);//是否允许使用缓存,这里不使用cache
webView.loadUrl(URL);//加载网页
webView.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onReceivedTitle(WebView view, String title) {
// TODO Auto-generated method stub
//Qiuchangdaohang.this.setTitle(title);
super.onReceivedTitle(view, title);
}
@Override
public void onProgressChanged(WebView view, int newProgress) {
// TODO Auto-generated method stub
progressBar.setVisibility(1);
progressBar.setProgress(newProgress);
progressBar.postInvalidate();
if (newProgress == 100) {
progressBar.setVisibility(View.GONE);//完全隐藏
newProgress=0;
}
super.onProgressChanged(view, newProgress);
}
});
}
}
});
android webview重新装载url,只需要再写一次loadurl即可,你可以监听页面是否加载完,提示一个加载进度条。