Android Webview加载页面
2017-06-29 21:23
323 查看
public class Webviews extends AppCompatActivity implements View.OnClickListener { private TextView titleTxt; private ProgressBar progressBar; private WebView webView; String url="http://mini.eastday.com/mobile/170621135553962.html"; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.webviews); initView(); webView.loadUrl(url); } private void initView() { titleTxt=(TextView)findViewById(R.id.title); progressBar=(ProgressBar)findViewById(R.id.progressbar); webView=(WebView)findViewById(R.id.webview); webView.setWebChromeClient(new WebChromeClient(){ @Override public void onProgressChanged(WebView view, int newProgress) { super.onProgressChanged(view, newProgress); progressBar.setProgress(newProgress); } @Override public void onReceivedTitle(WebView view, String title) { super.onReceivedTitle(view, title); titleTxt.setText("Webview"); } }); webView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); //true 自己处理 false浏览器出处理 默认false return true; } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); progressBar.setVisibility(View.VISIBLE); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view,url); progressBar.setVisibility(View.GONE); } }); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.left: goback(); break; case R.id.right: webView.reload(); webView.goForward(); break; default: break; } } private void goback() { if(webView.canGoBack()){ webView.goBack(); }else{ webView.clearView(); finish(); } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if(keyCode==KeyEvent.KEYCODE_BACK){ if(webView.canGoBack()){ webView.goBack(); return true; } } return super.onKeyDown(keyCode, event); }}
------------------------------------------
activity布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:layout_alignParentLeft="true"
android:text="后退"/>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="主题"/>
<Button
android:id="@+id/right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:onClick="onClick"
android:text="前进"/>
</RelativeLayout>
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
style="?android:attr/progressBarStyleHorizontal"/>
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
相关文章推荐
- Android Webview 加载一个页面时,报错 java.lang.Throwable: EventHub.removeMessages(int what = 105) is not suppo
- Android WebView加载服务端页面AJAX认证失败问题
- 毕业设计之android混合模式开发第一天--具有下拉刷新和页面加载等待的WebView搭建
- 【Android】WebView加载https页面不能正常显示资源问题
- Android webview加载Html页面,传参到Html并获取返回值
- Android菜鸟学习笔记(WebView加载html页面,页面提交数据问题)
- Android WebView Cookie的相关设置和自定义错误页面加载
- 【转】Android开发必知--WebView加载html5实现炫酷引导页面
- Android WebView页面加载优化
- android 加载webview 自定义 失败页面
- Android webview 加载html 页面缩放的问题
- android,使用webView加载页面,界面空隙问题
- android webview loaddata 或loadDataWithBaseURL加载页面空白
- Android WebView加载页面的输入框被软键盘遮挡的问题
- android webview 页面加载拦截
- Android WebVIew 加载页面失败
- android webView加载页面时显示出全部网页内容
- android webView加载页面时显示出全部网页内容
- Android开发必知--WebView加载html5实现炫酷引导页面
- Android WebView页面加载优化