Android Web View to Android Java Activity Class

孤街浪徒 提交于 2020-01-05 07:16:19

问题


is it possible to make a html button that when I click, it will go to my android activity class. Just like using Intent when I view Activity to another Activity

anyone have a thoughts?

my JsInteface class change into this

 public class JavaScriptInterface {
    Context mContext;

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


    /** Show a toast from the web page */
    public void showToast(String toast) {
        Intent mainIntent = new Intent(mContext, echos.class); 
        mContext.startActivity(mainIntent); 

    }

}

回答1:


public class JavaScriptInterface {
Context mContext;

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

/** Show a toast from the web page */
public void showToast(String toast) {
    Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}

 WebView webView = (WebView) findViewById(R.id.webview);
 webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");

in java script

   <input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

  <script type="text/javascript">
    function showAndroidToast(toast) {
    Android.showToast(toast);
   }
  </script>


来源:https://stackoverflow.com/questions/12191066/android-web-view-to-android-java-activity-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!