【问题标题】:My EditText won't add a point to my score. android quiz app我的 EditText 不会给我的分数加分。安卓测验应用
【发布时间】:2016-11-19 18:22:35
【问题描述】:

为什么当我提交正确答案时,我的程序没有给我的分数加分。 请帮忙。 这是我的变量:

public class MainActivity extends AppCompatActivity {
    int mscore =0 ;
    String q5R = "ripken";
    String q5CR = "cal ripken";
    String q5Cal = "cal";

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


    public void checkResult(View v) {
        //Access the RadioGroup view and save it to a variable.
        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.q1RadioBox);

        //Get the id of the RadioButton that is checked and save it
        //as an integer variable.
        int solutionId = radioGroup.getCheckedRadioButtonId();

        //Use if statements to respond based on whether
        //it is the id of the correct answer.
        if (solutionId == R.id.answer1) {
            mscore = mscore +1;
        } else {
                   mscore = mscore + 0;
               }

        //Access the RadioGroup2 view and save it to a variable.
        RadioGroup radioGroup2 = (RadioGroup) findViewById(R.id.q2RadioBox);

        //Get the id of the RadioButton that is checked and save it
        //as an integer variable.
        int solutionId2 = radioGroup2.getCheckedRadioButtonId();

        //Use if statements to respond based on whether
        //it is the id of the correct answer.
        if (solutionId2 == R.id.answer2) {
            mscore = mscore +1;
        } else {
                   mscore = mscore + 0;
               }

        CheckBox ws1966 = (CheckBox) findViewById(R.id.checkbox_1966);
        CheckBox ws1983 = (CheckBox) findViewById(R.id.checkbox_1983);
        CheckBox ws1970 = (CheckBox) findViewById(R.id.checkbox_1970);
        CheckBox ws1984 = (CheckBox) findViewById(R.id.checkbox_1984);
        CheckBox ws1961 = (CheckBox) findViewById(R.id.checkbox_1961);

        // Check for Required Answers
        if (ws1966.isChecked() && ws1983.isChecked() && ws1970.isChecked() && !ws1984.isChecked() && !ws1961.isChecked()) {
            mscore = mscore + 3;
        }

        EditText questionFive = (EditText) findViewById(R.id.question_5);
        String questionFiveAnswer = questionFive.getText().toString();
        if (questionFiveAnswer.equalsIgnoreCase(q5R) || questionFiveAnswer.equalsIgnoreCase(q5CR) || questionFiveAnswer.equalsIgnoreCase(q5Cal) ){
            mscore = mscore + 1;
        }

        EditText enterName = (EditText) findViewById(R.id.name_entry);
        String name = enterName.getText().toString();

        Context context = getApplicationContext();
        CharSequence text = name + " your score is: " + mscore + " points out of a possible 6 points";
        int duration = Toast.LENGTH_LONG;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }


}

【问题讨论】:

  • 这段代码是在你的activity onCreate()下还是onClick()下
  • 公共类 MainActivity 扩展 AppCompatActivity { int mscore ;字符串 q5R = "ripken";字符串 q5CR = "cal ripken";字符串 q5Cal = "cal";
  • 我已经对其进行了编辑,因此我的整个代码都在问题的上方。
  • 似乎 if 语句由于某种原因没有执行。
  • 我的单选按钮和复选框正在为分数添加分数,而不是我的 edittext 问题。

标签: java android android-edittext radio-group


【解决方案1】:

在布局文件中创建一个按钮,如:

<Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Submit"
  android:onClick="calcScore"
  android:id="@+id/button"/>

现在在主要活动的 calcScore 方法中编写您的代码,例如:

public void calcScore(View v)
{
    int mscore ;
String q5R = "ripken";
String q5CR = "cal ripken";
String q5Cal = "cal";



EditText questionFive = (EditText) findViewById(R.id.question_5);
    String questionFiveAnswer = questionFive.getText().toString();
    if (questionFiveAnswer.equalsIgnoreCase(q5R) ||         questionFiveAnswer.equalsIgnoreCase(q5CR) || questionFiveAnswer.equalsIgnoreCase(q5Cal) ){
         mscore = mscore + 1;
    }
}

【讨论】:

  • 404 页面未找到 :(
  • 查看github.com/danahigz/quiz 我没有看到任何 xml 布局文件。 R.layout.activity_main 在哪里?
  • 我发现这段代码没有任何问题。所以简单地复制到android studio并运行。它工作得很好。这段代码没有错。只要确保您使用按钮的 onClick 属性调用 checkResult 即可。我认为没有其他任何问题。
【解决方案2】:

您需要先初始化 int mscore。

// initialize
int mscore = 0; 

// than add it
mscore += 1;

【讨论】:

    【解决方案3】:

    我不确定出了什么问题。但它现在似乎工作得很好。感谢大家抽出宝贵时间提供帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-14
      • 1970-01-01
      相关资源
      最近更新 更多