android js 互調

來源:互聯網
上載者:User

標籤:

public class BoatsActivity extends Activity {    Handler mHandler = new Handler();//處理訊息的handler    @SuppressLint("SetJavaScriptEnabled")    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        this.setTitle("船舶查詢頁面");        final WebView mWebView = new WebView(this);//建立一個webView對象出來  將他作為activity的顯示頁面  你也可以在xml布局檔案中放入一個webView控制項在這裡初始化        setContentView(mWebView);        mWebView.loadUrl("http://192.168.0.190:8080/loginDemo_gd/bots.html");//載入你要顯示的網頁        mWebView.getSettings().setJavaScriptEnabled(true);//擷取webview對象中的setting對象 設定為可以支援javascript        mWebView.addJavascriptInterface(new Object() {             @JavascriptInterface//sdk版本大於17要加上這句註解 訪問安全考慮            public void clickOnAndroid() {                 mHandler.post(new Runnable() {                     public void run() {                         Toast.makeText(BoatsActivity.this, "請輸入你要查詢的船舶", Toast.LENGTH_SHORT).show();                    }                 });             }            @JavascriptInterface            public void showmsg(){                mHandler.post(new Runnable() {                     public void run() {                         Toast.makeText(BoatsActivity.this, "查詢成功", Toast.LENGTH_SHORT).show();                    }                 });             }            @JavascriptInterface            public void showResult(){                mHandler.post(new Runnable() {                     public void run() {                         Toast.makeText(BoatsActivity.this, "沒有查詢到對應的資料", Toast.LENGTH_SHORT).show();                    }                 });             }            @JavascriptInterface            public void alert(){                mHandler.post(new Runnable() {                     public void run() {                         Toast.makeText(BoatsActivity.this, "請輸入船舶的ID", Toast.LENGTH_SHORT).show();                    }                 });             }                    }, "demo");//類的別名  js這樣調用  window.demo.方法名(js調用java方法) 

      //java調用js
      webView.loadUrl("javascript:window.handler.show(document.getElementById(‘id‘).value,document.getElementById(‘password‘).value)");

        mWebView.setWebViewClient(new WebViewClient(){            @Override            public void onPageFinished(WebView view, String url) {                //Toast.makeText(BoatsActivity.this, "載入完成", Toast.LENGTH_SHORT).show();                super.onPageFinished(view, url);            }        });    }}

 

android js 互調

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.