簡體   English   中英

單擊“復選框”或WebView中的圖像以打開新的Android活動

[英]Click CheckBox or Image in WebView to open a new Android Activity

我創建了一個webview ,其中包含一個checkbox和一個image 我需要能夠選中該checkbox或單擊webviewimage以打開一個新activity 例如,如果我單擊screen1.htmlcheckboximage ,它必須加載Screen2Activity.java ,它將顯示第二個webview screen2.html

screen1.html

<html>
<head>
    <meta content="text/html; charset=UTF-8" http-equiv="content-type">
    <style>
        body {
            background-color: black;
            color: white;
            text-align:center;
        }
        header{
            color: white;
            font-family: verdana;
            font-size: 38pt;
            text-align:center;
        }
        subhead {
            color:red;
            background-color: black;
            font-family:verdana;
            font-size: 28pt;
            text-align:center;
        }
        normal {
            color: white;
            font-family: verdana;
            font-size: 11pt;
            text-align:center;
            border: 1px white;
            margin: 10px;
        }
    </style>
</head>
<body>
    <header>Screen1</header>
    <subhead>TYPE OF PROBLEM</subhead>
    <br><br><br><br><br><br>

    <normal>
    <input type="checkbox" name="1" value="1"> Is this to do with you witnessing other people harming other people?
    <br>
    (OTHERS TO OTHERS) goto Y
    <br><br>
    <a href="screen2.html"><img src="image1.gif" title="Home" width=200pt height=200pt /></a>
    <br><br><br><br><br>


    <input type="checkbox" name="2" value="2">Is this to do with yourself affecting others?
    <br>
    (SELF TO OTHERS) goto X
    <br><br>
    <a href="screen2.html"><img src="image11.gif" title="Home" width=200pt height=200pt /></a>
    <br><br><br><br><br>


    <input type="checkbox" name="3" value="3"> Is this to do with yourself only?
    <br>
    (SELF TO SELF) goto Z
    <br><br>
    <a href="screen2.html"><img src="image20.gif" title="Home" width="140" height="109" /></a>
    <br><br><br><br><br>


    <input type="checkbox" name="4" value="4"> Is this to do with someone (others) affecting you?
    <br>
    (OTHERS TO SELF)
    <br><br>
    <a href="screen2.html"><img src="image22.gif" title="Home" width="140" height="109" /></a>
    <br><br><br><br><br>


    <input type="checkbox" name="5" value="5"> Is this to do with a Force affecting yourself?
    <br>
    (FORCES TO SELF)
    <br><br>
    <a href="screen2.html"><img src="image16.gif" title="Home" width="140" height="109" /></a>
    <br><br><br><br><br>

    <br>
    <input type="checkbox" name="main" value="main"> Main menu
    <br><br>
    <a href="screen2.html"><img src="image4.gif" title="Home" width="140" height="109" /></a>
    </normal>
</body>
</html>

Screen1Actvity.java

package com.example.warre.finalappwritings;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;

public class Screen1Activity extends AppCompatActivity {

WebView webView;

@SuppressLint("JavascriptInterface")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_screen1);

    webView = (WebView) findViewById(R.id.webview);
    webView.loadUrl("file:///android_asset/screen1.html");

    startService(new Intent(Screen1Activity.this, SoundService.class));
}

@Override
protected  void onStop(){
    stopService(new Intent(Screen1Activity.this, SoundService.class));
    super.onStop();
}

@Override
protected void onStart() {
    startService(new Intent(Screen1Activity.this, SoundService.class));
    super.onStart();
}
}

您可以使用Java腳本調用方法

在HTML方面:

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

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

在本機上:

public class WebAppInterface {
    Context mContext;

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

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

Google的文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM