Android Studio测验应用程序没有Checked Radiobutton

时间:2016-01-18 09:24:54

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

嗨好日子那里这是我在我的按钮中的条件接下来我的问题是我有一个祝酒词,如果在一个radiogroup中没有选择任何东西那么它会弹出一条消息,但它没有做任何事情,当我按下它时它会去下一个问题 我想要的是当没有选择radiobuttons它只会弹出一条消息,问题将是相同的,它不会去下一个问题这里是我使用的代码我认为问题是我放置条件的地方检查它是否没有任何已检查的答案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()
    {

    }

}

2 个答案:

答案 0 :(得分:1)

如果条件失败,则不应将条件放在else中,因为它只在第一次执行时才会执行。将它放在一个单独的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++;
       }
}

答案 1 :(得分:0)

试试这个:

String Id=String.valueOf(radioGroup.getCheckedRadioButtonId());

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

我希望这会对你有所帮助。