public class WebActivity extends Activity implements View.OnClickListener {
private WebView webView;
private EditText edit_web_adds;
private Button btn_webadds_go, btn_web_back;
private ProgressBar pb;
String Url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
init();
btn_webadds_go.setOnClickListener(this);
btn_web_back.setOnClickListener(this);
//设置webview支持javascrap
webView.getSettings().setJavaScriptEnabled(true);
//设置webview支持缩放
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
//使用缓存
webView.getSettings().setAppCacheMaxSize(1024*10);//设置最大允许缓存
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
private void init() {
edit_web_adds=(EditText)this.findViewById(R.id.edit_web_adds);
btn_webadds_go=(Button)this.findViewById(R.id.btn_webadds_go);
btn_web_back=(Button)this.findViewById(R.id.btn_web_back);
webView=(WebView)this.findViewById(R.id.webView);
pb=(ProgressBar) findViewById(R.id.pb);
pb.setMax(100);
}
private class xWebViewClient extends WebChromeClient {
public void onProgressChanged(WebView view, int newProgress) {
pb.setProgress(newProgress);
if (newProgress == 100) {
pb.setVisibility(View.GONE);
}
super.onProgressChanged(view, newProgress);
}
}
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_webadds_go:
Url=edit_web_adds.getText().toString();
//覆盖webview默认使用第三方浏览器打开网页的行为,而使用本webview打开
webView.setWebViewClient(new android.webkit.WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//view.loadUrl(url);
return false; //设为false时,当按物理返回键可以从重定向的页面返回到重定向前的页面
}
});
webView.setWebChromeClient(new xWebViewClient());
webView.loadUrl("http://" + Url);
//隐藏软键盘
InputMethodManager im=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(view.getWindowToken(),0);
break;
case R.id.btn_web_back:
WebActivity.this.finish();
break;
default:
break;
}
}
//改写物理按键——返回键的逻辑
@Override
public boolean onKeyDown(int keyCode,KeyEvent event){
if (keyCode==KeyEvent.KEYCODE_BACK){
if (webView.canGoBack()){
webView.goBack();//返回上一页面
return true;
}
else {
//System.exit(0);//直接退出程序
this.onDestroy();//退出当前activity(WebActivity)
}
}
return super.onKeyDown(keyCode,event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_web, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
麻烦看看,怎样可以点击webview里面的链接时,也能显示进度条,并且获取此链接给EditText ?