您好,登錄后才能下訂單哦!
Android WebView的使用方法
Android app打開H5頁一般要實現如下需求:
1、打開指定url網頁;
2、點擊鏈接可以跳轉到下一頁,并更新標題;
3、按back鍵或左箭頭可以返回上一頁;
4、當webview顯示的是第一級url時, 按返回鍵或左箭頭關閉當前界面;
5、WebView如何傳值給android, 例如使用H5登錄成功后返回姓名、token等等字段。
6、支持JavaScript, 支持顯示js對話框。
7、無網絡時顯示默認布局, 以提高用戶體驗。
8、避免WebView的內存泄漏。
不多說, 看下面代碼如何實現上述功能。
參考布局:
<?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防止內存泄露隱患--> <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(); //設置WebView屬性,能夠執行Javascript腳本 webSettings.setJavaScriptEnabled(true); //設置可以訪問文件 webSettings.setAllowFileAccess(true); //設置支持縮放 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默認不會顯示JavaScript的Alert,需要Android實現。 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();//這里必須調用,否則頁面會阻塞造成假死 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里放置想要的參數 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); //優化體驗,避免顯示錯誤信息 } 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的使用方法詳解,大家如果有疑問可以留言討論,或者到本站社區交流,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。