Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android HTML按钮onclick事件_Android_Html_Android Activity_Android Webview_Android Websettings - Fatal编程技术网

Android HTML按钮onclick事件

Android HTML按钮onclick事件,android,html,android-activity,android-webview,android-websettings,Android,Html,Android Activity,Android Webview,Android Websettings,我有图像显示在本地HTML页面,并有一个按钮点击事件。单击此按钮时,我需要调用另一个活动。它起作用了。但问题是,在经历了另一次之后,返回到HTML页面,图像没有显示,应用程序也不工作 <script language="javascript"> function validClick() { valid.performClick(); document.getElementById("ok").value = "start"; } function refuseCli

我有图像显示在本地HTML页面,并有一个按钮点击事件。单击此按钮时,我需要调用另一个活动。它起作用了。但问题是,在经历了另一次之后,返回到HTML页面,图像没有显示,应用程序也不工作

<script language="javascript">
function validClick() {
    valid.performClick();
    document.getElementById("ok").value = "start";
}
function refuseClick(){
    refuse.performClick();
    document.getElementById("no").value = "Je refuse";
}

<div>
<button type="button" id="ok"
    style="font-weight: 700; margin-right: 20px;" onclick="validClick();">Start</button>

</div>

您需要检查代码中的一些内容

首先,在HTML中,顶部的
标记没有关闭。请在上次函数定义后添加一个

其次,在Java中,请在调用
loadUrl
之前配置您的WebView。应首先注册设置和JavaScript接口

第三,不需要使用
按钮
小部件来实现您想要的效果。像这样的东西应该足够了:

webview.addJavaScriptInterface(new Object() {
    @JavaScriptInterface
    public void performClick() {
        Intent i = new Intent(this, CloudReco.class);
        startActivity(i);
    }
}, "valid");
与“拒绝”类似。

下一个“奇怪”方法是我用来传递很多javascript事件的(上次使用的是fro scroll event,我传递的也是连接的值):

HTML包含一个虚拟
元素,用于模拟URL单击,其中URL是我们在本机代码中等待的键:

<!DOCTYPE html>
<html>
<body>
<a id='yourLinkID'></a>

<button onclick="myFunction('button1Event')">Button1</button>
<button onclick="myFunction('button2Event')">Button2</button>

<script>
function myFunction(value) {
    document.getElementById('yourLinkID').href = 'testtest' + value; 
    document.getElementById('yourLinkID').click();
// we set the wanted URL and simulate the click
}
</script>

</body>
</html>

按钮1
按钮2
函数myFunction(值){
document.getElementById('yourLinkID')。href='testtest'+值;
document.getElementById('yourLinkID')。单击();
//我们设置想要的URL并模拟点击
}

您能否澄清图像不显示和应用程序不工作?你列出的HTML中没有图像。你能检查编辑过的代码吗?我仍然不能确切地说出你所说的应用程序不起作用是什么意思,但我已经发布了一些提示,这些提示应该会对你的程序有所改进。如果您仍然有问题,请张贴问题的确切细节;“它不起作用”太含糊了!:-)
webview.addJavaScriptInterface(new Object() {
    @JavaScriptInterface
    public void performClick() {
        Intent i = new Intent(this, CloudReco.class);
        startActivity(i);
    }
}, "valid");
webView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.contains("button1Event")) {
            // button1 was clicked inside webview html
            Toast.makeText(context, "Button1WasClicked", Toast.LENGTH_SHORT).show();  
            return true; //(to avoid a default redirect to the url)
         } else if (url.contains("button2Event")) {
            // button2 was clicked inside webview html
            Toast.makeText(context, "Button2WasClicked", Toast.LENGTH_SHORT).show();  
            return true; //(to avoid a default redirect to the url)
         }
         return false;
      }
}
<!DOCTYPE html>
<html>
<body>
<a id='yourLinkID'></a>

<button onclick="myFunction('button1Event')">Button1</button>
<button onclick="myFunction('button2Event')">Button2</button>

<script>
function myFunction(value) {
    document.getElementById('yourLinkID').href = 'testtest' + value; 
    document.getElementById('yourLinkID').click();
// we set the wanted URL and simulate the click
}
</script>

</body>
</html>