电台组,两次计算答案

时间:2017-03-28 09:34:03

标签: android android-radiogroup

我的问题必须有一个简单的答案,我确定我错过了一些明显的东西......有人可以为我指出它吗?

当我在手机上运行应用程序时,广播组的行为正常(意味着错误答案的分数为0,正确答案的分数为1)但是如果我点击了错误答案,则返回要正确答案并重新提交,它会为分数增加一个。所以同样的问题只会显示两点,只是点击两次正确答案......

package com.example.android.englishquizl3;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import static com.example.android.englishquizl3.R.id.radioButton2;
import static com.example.android.englishquizl3.R.id.radioButton3;

public class MainActivity extends AppCompatActivity {

int baseScore = 0;
private RadioGroup radioGroup1;
private RadioGroup radioGroup2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initialize Radio Group and attach click handler
    radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
    radioGroup2 = (RadioGroup) findViewById(R.id.radioGroup2);
    radioGroup1.clearCheck();
    radioGroup2.clearCheck();

    // Attaches checkedChangeListener to radio group
    radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton your = (RadioButton) findViewById(radioButton2);

            if (your.isChecked()) {
                baseScore = baseScore + 1;
            } else {
            }

            int score = 1;
            if (your.isChecked()) score += 1;

            switch (score) {
                case R.id.radioButton2:
                    if (your.isChecked()) ;
                    score++;
                default:
                    break;
            }
        }
    });

    radioGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton theyre = (RadioButton) findViewById(radioButton3);

            if (theyre.isChecked()) {
                baseScore = baseScore + 1;
            } else {
            }

            int score = 1;
            if (theyre.isChecked()) score += 1;

            switch (score) {
                case R.id.radioButton3:
                    if (theyre.isChecked()) ;
                    score++;
                default:
                    break;
            }
        }
    });

}

public void onSubmit(View v) {
    RadioButton rb1 = (RadioButton) radioGroup1.findViewById(radioGroup1.getCheckedRadioButtonId());
    RadioButton rb2 = (RadioButton) radioGroup2.findViewById(radioGroup2.getCheckedRadioButtonId());
    Toast.makeText(MainActivity.this, "Score: " + baseScore, Toast.LENGTH_SHORT).show();
}
}

我的xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:text="1) I think _______ cat is lovely."
    android:textColor="#000000" />

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="you're" />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="your" />
</RadioGroup>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:text="2) _______ going to shop tomorrow."
    android:textColor="#000000" />

<RadioGroup
    android:id="@+id/radioGroup2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="They're" />

    <RadioButton
        android:id="@+id/radioButton4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Their" />
</RadioGroup>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:id="@+id/submitBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onSubmit"
        android:text="Submit" />

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

您只需在@Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException { HttpServletRequest httpRequest = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; if (!ENVIRONMENT_DEV.equalsIgnoreCase(environment)) { ispUtility = ISPUtils.getInstance(); ispFileProvider = ispUtility.getTempFileUtility(); String originAddress = httpRequest.getHeader("Origin"); if (originAddress != null) { if (originAddress.toLowerCase().contains(ispFileProvider.getIspDomainName().toLowerCase())) { log.info("Origin Match Found: "+originAddress.replaceAll("\r\n", "")); response.setHeader("Access-Control-Allow-Origin", originAddress.replaceAll("\r\n", "")); } else { response.setHeader("Access-Control-Allow-Origin", "https://localhost:8001"); } } } else { response.setHeader("Access-Control-Allow-Origin", "*"); } response.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, x-auth-token"); response.setHeader("Access-Control-Expose-Headers", "x-auth-token"); response.setHeader("Access-Control-Max-Age", "3600"); try { chain.doFilter(req, res); } catch (IOException e) { log.error("IOException Occurred :" + e.getMessage()); } catch (ServletException e) { log.error("IOException Occurred :" + e.getMessage()); } } @Override public void init(FilterConfig filterConfig) { } @Override public void destroy() { } 中添加1,因此每次更改状态并获得正确答案时,您需要再添加一个。如果检查错误的数字或将其设置为1

,则必须减少baseScore

答案 1 :(得分:0)

@Emily你的问题在切换的情况下有很多错误你在if条件中添加了分号,我不知道为什么,因为在这种情况下有一个if语句是没有意义的。

在选中已更改后,您将添加已经得分为1的变量。

我的建议是你最好采用一个全局变量来跟踪得分。