【发布时间】: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()
{
}
}
【问题讨论】:
-
您是否使用
RadioButton和RadioGroup..?你能发布你的代码吗? -
好吧,别发帖了
标签: android performance android-layout android-fragments android-intent