您好,登錄后才能下訂單哦!
Android WebView的使用方法
Android app打開H5頁一般要實現(xiàn)如下需求:
1、打開指定url網(wǎng)頁;
2、點擊鏈接可以跳轉(zhuǎn)到下一頁,并更新標(biāo)題;
3、按back鍵或左箭頭可以返回上一頁;
4、當(dāng)webview顯示的是第一級url時, 按返回鍵或左箭頭關(guān)閉當(dāng)前界面;
5、WebView如何傳值給android, 例如使用H5登錄成功后返回姓名、token等等字段。
6、支持JavaScript, 支持顯示js對話框。
7、無網(wǎng)絡(luò)時顯示默認(rèn)布局, 以提高用戶體驗。
8、避免WebView的內(nèi)存泄漏。
不多說, 看下面代碼如何實現(xiàn)上述功能。
參考布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:my="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/activity_main_bg" android:orientation="vertical"> <com.eloancn.borrower.common.widget.TitleView android:id="@+id/titleView" android:layout_width="match_parent" android:layout_height="50dp" my:titleText="H5" /> <RelativeLayout android:id="@+id/rl_webViewContainer" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!--在代碼中添加webView防止內(nèi)存泄露隱患--> <LinearLayout android:id="@+id/neterror_tip" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:visibility="gone"> <ImageView android:layout_marginTop="110dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/not_found" /> </LinearLayout> </RelativeLayout> </LinearLayout>
示例代碼:
public class CommonWebViewActivity extends Activity { private WebView mWebView; private TitleView mTitleView; private RelativeLayout mWebViewContainer; private String title; private String url; private LinearLayout neterrorLayout; private int mFlag; //來源 public static final int FLAG_SIGNATURE = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_common_webview_main); initData(); initView(); setData(); } @Override protected void onDestroy() { super.onDestroy(); mWebView.setWebViewClient(null); mWebView.setWebChromeClient(null); mWebViewContainer.removeView(mWebView); mWebView.removeAllViews(); mWebView.destroy(); } private void initData() { url = getIntent().getStringExtra("url"); title = getIntent().getStringExtra("title"); mFlag = getIntent().getIntExtra("flag", 0); } private void initView() { mTitleView = (TitleView) findViewById(R.id.titleView); mWebViewContainer = (RelativeLayout) findViewById(R.id.rl_webViewContainer); neterrorLayout = (LinearLayout) findViewById(R.id.neterror_tip); mWebView = new WebView(getApplicationContext()); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); mWebViewContainer.addView(mWebView, layoutParams); WebSettings webSettings = mWebView.getSettings(); //設(shè)置WebView屬性,能夠執(zhí)行Javascript腳本 webSettings.setJavaScriptEnabled(true); //設(shè)置可以訪問文件 webSettings.setAllowFileAccess(true); //設(shè)置支持縮放 webSettings.setBuiltInZoomControls(false); //允許js彈出窗口 webSettings.setJavaScriptCanOpenWindowsAutomatically(true); mTitleView.setLeftBtnClickListener(new TitleView.OnBtnClickListener() { @Override public void onClick(View v) { if (mWebView.canGoBack()) { mWebView.goBack(); } else { finish(); } } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (mWebView.canGoBack()) { mWebView.goBack(); return true; } } return super.onKeyDown(keyCode, event); } private void setData() { mTitleView.setTitle(title); mWebView.loadUrl(url); mWebView.setWebChromeClient(new WebChromeClient(){ @Override public boolean onJsAlert(WebView view, String url, final String message, final JsResult result) { //注意,WebView默認(rèn)不會顯示JavaScript的Alert,需要Android實現(xiàn)。 runOnUiThread(new Runnable() { @Override public void run() { //自定義美觀的Dialog,僅僅是為了顯示message CustomDialog.Builder builder = new CustomDialog.Builder(CommonWebViewActivity.this); builder.setTitle("提示"); builder.setMessage(message); builder.setSingle("知道了", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss(); } }); builder.create().show(); } }); result.confirm();//這里必須調(diào)用,否則頁面會阻塞造成假死 return true; } @Override public void onReceivedTitle(final WebView view, final String title) { super.onReceivedTitle(view, title); if (FLAG_SIGNATURE == mFlag) { runOnUiThread(new Runnable() { @Override public void run() { mTitleView.setTitle(title); } }); } } }); mWebView.setWebViewClient(new WebViewClient() { @Override public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { if (FLAG_SIGNATURE == mFlag) { Log.d("brycegao", "shouldInterceptRequest url:" + request.getUrl().toString()); if (request.getUrl().toString().contains("wxd://getImage?tenderid=")) { //可以通過url傳值給Android, 即在url里放置想要的參數(shù) runOnUiThread(new Runnable() { @Override public void run() { setResult(RESULT_OK); finish(); } }); } } return super.shouldInterceptRequest(view, request); } @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { mWebView.loadUrl(url); return true; } @Override public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); if (FLAG_SIGNATURE == mFlag && request.getUrl().toString().contains("wxd://getImage?tenderid=")) { //do nothing mWebView.setVisibility(View.GONE); //優(yōu)化體驗,避免顯示錯誤信息 } else { //加載失敗 neterrorLayout.setVisibility(View.VISIBLE); mWebView.setVisibility(View.GONE); } } }); neterrorLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mWebView.setVisibility(View.VISIBLE); neterrorLayout.setVisibility(View.GONE); mWebView.loadUrl(url); } }); } }
以上就是Android Webview的使用方法詳解,大家如果有疑問可以留言討論,或者到本站社區(qū)交流,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。