android WebView 显示网页
生活随笔
收集整理的這篇文章主要介紹了
android WebView 显示网页
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><include layout="@layout/uzone_top_bar" /><WebViewandroid:id="@+id/webview"android:layout_width="match_parent"android:layout_height="match_parent" /></LinearLayout> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="40dp"android:gravity="center_vertical" ><TextViewandroid:id="@+id/uzone_top_TextView_title"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_centerVertical="true"android:gravity="center"android:text=""android:textSize="18sp" /><RelativeLayoutandroid:id="@+id/uzone_top_RelativeLayout_cancel"android:layout_width="50dp"android:layout_height="match_parent"android:layout_alignParentLeft="true"android:layout_centerVertical="true"android:gravity="center" ><ImageViewandroid:id="@+id/uzone_top_ImageView_cancel"android:layout_width="20dp"android:layout_height="20dp"android:layout_marginLeft="10dp"android:paddingLeft="10dp"android:paddingTop="10dp" /></RelativeLayout><ImageViewandroid:id="@+id/uzone_top_ImageView_line"android:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_marginRight="2dp"android:layout_toLeftOf="@+id/uzone_top_Button_ok"android:visibility="gone" /><Buttonandroid:id="@+id/uzone_top_Button_ok"android:layout_width="70dip"android:layout_height="match_parent"android:layout_alignParentRight="true"android:layout_gravity="center"android:text="按鈕"android:textColor="@android:color/white"android:textSize="18sp"android:visibility="gone" /></RelativeLayout> public class WebBrowserActivity extends Activity
{/*** UshequMobile地址*/public static final String URL_PREFIX = "http://10.10.9.51:8080/UshequMobile/";public static final String USERAGENT = "haiersoft.webbrowser";private WebView webView;private TextView title; @Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.web_browser);//標題title = (TextView) findViewById(R.id.uzone_top_TextView_title);//返回按鈕RelativeLayout back = (RelativeLayout) findViewById(R.id.uzone_top_RelativeLayout_cancel);back.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {finish();}});webView = (WebView) findViewById(R.id.webview);configWebView();Intent intent = getIntent();String url = intent.getStringExtra("url");if(null != url){webView.loadUrl(url);}else{Toast.makeText(this, "url為空", Toast.LENGTH_SHORT).show();}}private void configWebView(){WebSettings webSettings = webView.getSettings();
webSettings.setSupportZoom(false);//設置不可縮放webSettings.setJavaScriptEnabled(true); //設置支持javascriptwebSettings.setUserAgentString(USERAGENT);//設置值用于Web服務判斷訪問來源
//WebViewClient就是幫助WebView處理各種通知、請求事件的,具體來說包括:?webView.setWebViewClient(new WebViewClient(){public void onPageFinished(WebView view, String url) {super.onPageFinished(view, url);}public void onPageStarted(WebView view, String url, Bitmap favicon) {super.onPageStarted(view, url, favicon);}@Overridepublic void onReceivedError(WebView view, int errorCode, String description, String failingUrl){super.onReceivedError(view, errorCode, description, failingUrl);Toast.makeText(getApplicationContext(), description, Toast.LENGTH_LONG).show();}});
//WebViewClient就是幫助WebView處理各種通知、請求事件的,具體來說包括:?webView.setWebViewClient(new WebViewClient(){public void onPageFinished(WebView view, String url) {super.onPageFinished(view, url);}public void onPageStarted(WebView view, String url, Bitmap favicon) {super.onPageStarted(view, url, favicon);}@Overridepublic void onReceivedError(WebView view, int errorCode, String description, String failingUrl){super.onReceivedError(view, errorCode, description, failingUrl);Toast.makeText(getApplicationContext(), description, Toast.LENGTH_LONG).show();}});
//WebChromeClient主要處理解析,渲染網頁等瀏覽器做的事情
//WebChromeClient是輔助WebView處理Javascript的對話框,網站圖標,網站title,加載進度等?
webView.setWebChromeClient(new WebChromeClient() //游覽器{@Overridepublic boolean onJsAlert(WebView view, String url, String message, JsResult result) //對話框{ // result.confirm(); // return true;return super.onJsAlert(view, url, message, result);}@Overridepublic void onReceivedTitle(WebView view, String title) //標題{super.onReceivedTitle(view, title);WebBrowserActivity.this.title.setText(title);}});}@Overrideprotected void onDestroy(){super.onDestroy();}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) //按鍵響應{if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) { webView.goBack(); return true; } return super.onKeyDown(keyCode, event); }@Overrideprotected void onPause(){super.onPause();}@Overrideprotected void onRestart(){super.onRestart();}@Overrideprotected void onResume(){super.onResume();}}?
轉載于:https://www.cnblogs.com/songyao/p/4169632.html
總結
以上是生活随笔為你收集整理的android WebView 显示网页的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 修改lastpass主密码后需重启fir
- 下一篇: 【C++】const成员函数