单击WebView中的CheckBox或Image以打开新的Android Activity

时间:2018-04-26 07:37:46

标签: android html android-webview

我创建的webview包含checkboximage。我需要能够检查checkbox或点击image中的webview以打开新的activity。例如,如果我点击checkbox中的imagescreen1.html,则必须加载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();
}
}

1 个答案:

答案 0 :(得分:0)

您可以使用Javascript调用方法

在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's documentation