【问题标题】:Android Studio Quiz App No Checked RadiobuttonAndroid Studio Quiz App 未选中单选按钮
【发布时间】:2016-01-18 09:24:53
【问题描述】:

嗨,美好的一天,这是我的 Button 中的条件 next 我的问题是我有一个祝酒词,如果在 radiogroup 中没有选择任何内容,那么它会弹出一条消息,但它没有做任何事情,当我按下 next 时,它会转到下一个问题 我想要的是当未选择单选按钮时,它只会弹出一条消息,问题将是相同的,它不会进入下一个问题这里是我使用的代码我认为问题是我放置条件的地方检查它是否没有任何已检查的答案 idk 真的请帮助我

@Override
            public void onClick(View v) {
                RadioButton uans = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
                String ansText = uans.getText().toString();
                if (ansText.equalsIgnoreCase(answer[flag2])) {
                    while (numbers.contains(flag2)) {
                        flag2 = rnd.nextInt(4);
                    }
                    correct++;
                    coins++;
                }
                else if ((radioGroup.getCheckedRadioButtonId() == -1)
                {
                        Toast.makeText(getApplicationContext(), "Select an     Answer Please", Toast.LENGTH_LONG).show();
                }

                else
                {
                    wrong++;
                }
                flag++;
                flag2 = rnd.nextInt(4);
                if (flag < questions.length)
                {
                    tvQuestion.setText(questions[flag2]);
                    rb1.setChecked(false);
                    rb2.setChecked(false);
                }
                else
                {
                    Intent inResult = new Intent(getApplicationContext(), Results.class);
                    SharedPreferences saveGame = getSharedPreferences("Game", MODE_PRIVATE);
                    SharedPreferences.Editor editor = saveGame.edit();
                    editor.putInt("savedPlayerCredit", credit);
                    editor.putInt("savedPlayerCoins", coins);
                    editor.putInt("savedPlayerCorrect", correct);
                    editor.putInt("savedPlayerWrong", wrong);
                    editor.apply();
                    timer.cancel();
                    startActivity(inResult);
                    }
                }
            }

            );
        }

这是完整的代码

package org.intercode.lifeatceu;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.provider.MediaStore;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashSet;
import java.util.Random;

public class levelone extends AppCompatActivity {
    TextView tv1, tvCred, tvQuestion, tvTimer;
    Button btNext;
    RadioButton rb1, rb2;
    RadioGroup rg;
    LinearLayout ll;

    String questions [] = {"Ma. Cristina D. Padolina is CEU's President", "Carlito B. Olaer is the V.P of CEU", "CEU's VISION is to sting every enemy", "One of CEU's Mission is to promote creative and scholarly academic"};
    String answer [] = {"True", "False", "False", "True"};
    HashSet numbers = new HashSet();


    int flag = 0;
    Random rnd = new Random();
    int flag2 = 0 ;
    int correct = 0;
    int wrong = 0;
    int coins = 0;
    int time = 0;
    int credit = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_levelone);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        SharedPreferences loadGame = getSharedPreferences("Game", MODE_PRIVATE);
        credit = loadGame.getInt("savedPlayerCredit", 0);
        time = loadGame.getInt("savedPlayerTime", 0);


        tv1 = (TextView) findViewById(R.id.tv1);
        tvCred = (TextView) findViewById(R.id.tvCred);
        tvQuestion = (TextView) findViewById(R.id.tvQuestion);
        tvTimer = (TextView) findViewById(R.id.tvTimer);

        rb1 = (RadioButton) findViewById(R.id.rb1);
        rb2 = (RadioButton) findViewById(R.id.rb2);
        rg = (RadioGroup) findViewById(R.id.rg);

        btNext = (Button) findViewById(R.id.btNext);

        ll = (LinearLayout) findViewById(R.id.ll);

        flag2 = rnd.nextInt(4);
        numbers.add(flag2);
        tvQuestion.setText(questions[flag2]);
        tvCred.setText(String.valueOf(credit));

      final CountDownTimer timer = new CountDownTimer(time, 1000)
        {
            public void onTick (long millisUntilFinished)
            {
                tvTimer.setText ("Time remaining : " + millisUntilFinished/1000);
            }

            @Override
            public void onFinish() {

                Intent inResult = new Intent(getApplicationContext(), Results.class);
                SharedPreferences saveGame = getSharedPreferences("Game", MODE_PRIVATE);
                SharedPreferences.Editor editor = saveGame.edit();
                editor.putInt("savedPlayerCredit", credit);
                editor.putInt("savedPlayerCoins", coins);
                editor.putInt("savedPlayerCorrect", correct);
                editor.putInt("savedPlayerWrong", wrong);
                editor.apply();
                startActivity(inResult);
            }
        }.start();
        btNext.setOnClickListener(new View.OnClickListener() {

                                      @Override
                                      public void onClick(View v) {
                                          RadioButton uans = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
                                          String ansText = uans.getText().toString();
                                          if (ansText.equalsIgnoreCase(answer[flag2])) {
                                              while (numbers.contains(flag2)) {
                                                  flag2 = rnd.nextInt(4);
                                              }
                                              correct++;
                                              coins++;
                                          }
                                  else if ((rg.getCheckedRadioButtonId() == -1)
                                       {
                                 Toast.makeText(getApplicationContext(), "Select an     Answer Please", Toast.LENGTH_LONG).show();
                            }
                                          else
                                          {
                                              wrong++;
                                          }
                                          flag++;
                                          flag2 = rnd.nextInt(4);
                                          if (flag < questions.length) {
                                              tvQuestion.setText(questions[flag2]);
                                              rb1.setChecked(false);
                                              rb2.setChecked(false);
                                          }
                                          else
                                          {
                                              Intent inResult = new Intent(getApplicationContext(), Results.class);
                                              SharedPreferences saveGame = getSharedPreferences("Game", MODE_PRIVATE);
                                              SharedPreferences.Editor editor = saveGame.edit();
                                              editor.putInt("savedPlayerCredit", credit);
                                              editor.putInt("savedPlayerCoins", coins);
                                              editor.putInt("savedPlayerCorrect", correct);
                                              editor.putInt("savedPlayerWrong", wrong);
                                              editor.apply();
                                              timer.cancel();
                                              startActivity(inResult);
                                          }
                                      }
                                  }

        );
    }

    @Override
    public void onBackPressed()
    {

    }

}

【问题讨论】:

标签: android performance android-layout android-fragments android-intent


【解决方案1】:

您不应该将条件放在 else if 中,因为它仅在第一个 if 条件失败时才执行。将其放在单独的 if 条件中并检查。

if ((radioGroup.getCheckedRadioButtonId() == -1) {
       Toast.makeText(getApplicationContext(), "Select an     Answer Please", Toast.LENGTH_LONG).show();
} else {
       RadioButton uans = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
       String ansText = uans.getText().toString();
       if (ansText.equalsIgnoreCase(answer[flag2])) {
           while (numbers.contains(flag2)) {
                 flag2 = rnd.nextInt(4);
           }
           correct++;
           coins++;
       }
       else
       {
            wrong++;
       }
}

【讨论】:

    【解决方案2】:

    试试这个:

    String Id=String.valueOf(radioGroup.getCheckedRadioButtonId());
    
    if(!Id.equals(null)){
       Intent.....
    }else{
       Toast.makeText(getApplicationContext(), "Select an Answer Please",
                      Toast.LENGTH_LONG).show();
    }
    

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-27
      • 1970-01-01
      • 2015-03-03
      • 2017-10-28
      • 2018-11-19
      • 1970-01-01
      相关资源
      最近更新 更多