将html按钮链接到java活动

时间:2017-08-06 17:35:35

标签: javascript java android html

我正在为我的书制作一个Android应用程序我有一个html页面我想在其上添加一个链接到这个Java代码的按钮

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // listeners of our two buttons
    View.OnClickListener handler = new View.OnClickListener() {
        public void onClick(View v) {
            switch (v.getId()) {

                case R.id.buttonShareTextUrl:
                    shareTextUrl();
                    break;


            }
        }
    };

    // our buttons
    findViewById(R.id.buttonShareTextUrl).setOnClickListener(handler);


}

// Method to share either text or URL.
private void shareTextUrl() {
    Intent share = new Intent(android.content.Intent.ACTION_SEND);
    share.setType("text/plain");
    share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

    // Add data to the intent, the receiving app will decide
    // what to do with it.
    share.putExtra(Intent.EXTRA_SUBJECT, "book");
    share.putExtra(Intent.EXTRA_TEXT, "https://play.google.com/store/apps/details?id=com.sohaibm.assrar_bac");

    startActivity(Intent.createChooser(share, "share with"));
}

}

所以我希望你给我一个页面的HTML代码,其中包含与此活动相关的按钮,非常感谢你

2 个答案:

答案 0 :(得分:2)

如果您使用WebView来展示图书内容,可以使用WebViewClient

WebViewClient mWebClient = new WebViewClient(){

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(url.startsWith("handle:")){
                // do something
                return true;
            }

            else{
                view.loadUrl(url);
            }
            return true;
        }
    };
    mWebView.setWebViewClient(mWebClient);

你的链接应该是:

<a href="handle://it.to.me">Link text</a>

答案 1 :(得分:0)

试试这样:

Html页面:

<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="style.css" />

        <script type="text/javascript">

        function moveToScreenTwo() {
        Android.moveToNextScreen();
        }
        </script>

    </head>

  <body>
     <div>
            <input type="button" value="Locate" onClick="moveToScreenTwo()" />
        </div>
  </body>
  </html>

<强> FirstACtivity.java:

import android.webkit.JavascriptInterface;

webView = (WebView) findViewById(R.id.load_url);
webView.getSettings().setJavaScriptEnabled(true);

 if (new File(url).exists()) {
            webView.loadUrl(FILENAME_PREFIX + url);
            Log.d("fileurl", "" + FILENAME_PREFIX + url);
  webView.addJavascriptInterface(new WebAppInterface(this), "Android");
        }
  //Class to be injected in Web page
    public class WebAppInterface {
        Context mContext;

        /**
         * Instantiate the interface and set the context
         */
        WebAppInterface(Context c) {
            mContext = c;
        }

        @JavascriptInterface
        public void moveToNextScreen() {

            Intent i = new Intent(FirstActivity.this,SecondActivity.class);
            startActivity(i);

        }
     }

更多参考:请检查this tutorial