Android的Web视图到Android的Java类活动 [英] Android Web View to Android Java Activity Class

查看:181
本文介绍了Android的Web视图到Android的Java类活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是有可能使一个HTML按钮,当我点击,它会去我的Andr​​oid活动类。
就像使用意图时,我认为活动到另一个活动

任何人有什么想法?

我JsInteface类切换到该

 公共类JavaScriptInterface {
    上下文mContext;    / **实例化界面和设置上下文* /
    JavaScriptInterface(上下文C){
        mContext = C;
    }
    / **显​​示从网页举杯* /
    公共无效showToast(字符串吐司){
        意图mainIntent =新意图(mContext,echos.class);
        mContext.startActivity(mainIntent);    }}


解决方案

 公共类JavaScriptInterface {
上下文mContext;/ **实例化界面和设置上下文* /
JavaScriptInterface(上下文C){
    mContext = C;
}/ **显​​示从网页举杯* /
公共无效showToast(字符串吐司){
    Toast.makeText(mContext,烤面包,Toast.LENGTH_SHORT).show();
}
} web视图的WebView =(的WebView)findViewById(R.id.webview);
 webView.addJavascriptInterface(新JavaScriptInterface(本),机器人);

在Java脚本

 <输入类型=按钮值=问好的onClick =showAndroidToast('您好!Android的')/>  <脚本类型=文/ JavaScript的>
    功能showAndroidToast(吐司){
    Android.showToast(面包);
   }
  < / SCRIPT>

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); 

    }

}

解决方案

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>

这篇关于Android的Web视图到Android的Java类活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆