WebView 實現JS效果和a標籤的點擊事件

來源:互聯網
上載者:User

標籤:ted   name   index   layout   cte   request   dex   嵌入式   支援   

目前很多android app都可以顯示web頁面的介面,嵌入式開發,這個介面一般都是WebView這個控制項載入出來的,學習該控制項可以為你的app開發提升擴充性。

先說下WebView的一些優點:

  1. 可以直接顯示和渲染web頁面,直接顯示網頁
  2. webview可以直接用html檔案(網路上或本地assets中)作布局
  3. 和JavaScript互動調用
  4. 網頁標籤的點擊事件

效果:(網頁頂部是JS效果滾動,4個模組可以實現點擊事件,可看到資訊提示)

       

 

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="${relativePackage}.${activityClass}" >       <WebView            android:layout_width="match_parent"            android:layout_height="match_parent"            android:id="@+id/webview"            /></RelativeLayout>

MainActivity.java

public class MainActivity extends Activity {    public String URL = "http://bajie.zhangwoo.cn/app.php?platform=android&appkey=5a379b5eed8aaae531df5f60b12100cfb6dff2c1&c=travel&a=home";    WebView webView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        webView = (WebView) findViewById(R.id.webview);        webView.loadUrl(URL);        initView();    }    @SuppressLint("SetJavaScriptEnabled") private void initView() {        // TODO Auto-generated method stub        webView.requestFocus();        webView.setHorizontalScrollBarEnabled(true);        webView.setVerticalScrollBarEnabled(true);        WebSettings web = webView.getSettings();        web.setJavaScriptEnabled(true);// 啟用支援javascript        web.setBuiltInZoomControls(true);        web.setSupportZoom(true); // 是否支援螢幕雙擊縮放,但是下邊的是前提        web.setDefaultTextEncodingName("utf-8");// 設定編碼格式        // 覆蓋WebView預設使用第三方或系統預設瀏覽器開啟網頁的行為,使網頁用WebView開啟        webView.setWebViewClient(new WebViewClient() {            @Override            public boolean shouldOverrideUrlLoading(WebView view, String url) {                // TODO Auto-generated method stub                // 傳回值是true的時候控制去WebView開啟,為false調用系統瀏覽器或第三方瀏覽器                if (url.indexOf("zwapp://showlist/?tab=zhoubian") != -1) {                                        Toast.makeText(getApplicationContext(), "周邊遊", 1).show();                                    } else if (url.indexOf("zwapp://showlist/?tab=gonglue") != -1) {                                        Toast.makeText(getApplicationContext(), "旅遊攻略", 1).show();                } else if (url.indexOf("zwapp://showlist/?tab=zhaiguo") != -1) {                                        Toast.makeText(getApplicationContext(), "摘果", 1).show();                } else if (url.indexOf("zwapp://showlist/?tab=gongyuan") != -1) {                                        Toast.makeText(getApplicationContext(), "主題公園", 1).show();                                    } else {                }                return true;            }        });    }}

 

記得加網路許可權

<uses-permission android:name="android.permission.INTERNET"/>

 

源碼下載:  

CSDN:http://download.csdn.net/detail/dickyqie/9705932

WebView 實現JS效果和a標籤的點擊事件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.