public class TrueFalseActivity extends Activity implements View.OnClickListener{
Button buttonClicked = null;
List<QuestionTrueFalse> questionTrueFalseList;
int myscore = 0;
int quid=0;
int id ;
TextView tv, tv1;
QuestionTrueFalse curQues;
Button b1, b2;
Button next, back;
Button button;
QuestionTrueFalse cur;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trfal);
tv=(TextView) findViewById(R.id.tv1);
b1=(Button) findViewById(R.id.b1);
b2=(Button) findViewById(R.id.b2);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
DbHelper db = new DbHelper(this);
questionTrueFalseList = db.getAllTrFalsQuestions();
if (questionTrueFalseList != null && questionTrueFalseList.size() != 0) {
curQues = questionTrueFalseList.get(quid);
try {
setQuestionView();
}catch (NullPointerException e)
{
Toast.makeText(getApplicationContext(),"Exception caught",Toast.LENGTH_LONG).show();
}
}
next=(Button) findViewById(R.id.forw);
back=(Button) findViewById(R.id.back);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button=(Button) v;
String buttonText=button.getText().toString();
if(curQues.getAnsw().equals(buttonText))
{
myscore++;
}
cur=curQues;
if(quid<19){
quid++;
curQues=questionTrueFalseList.get(quid);
setQuestionView();
}else{
Intent intent = new Intent(TrueFalseActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", myscore); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
finish();
}
}
});
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (quid > 0) {
quid--;
curQues = questionTrueFalseList.get(quid);
setQuestionView();
}
}
});
private void setQuestionView() {
tv.setText(curQues.getQues());
b1.setText(curQues.getOpt1());
b2.setText(curQues.getOpt2());
}
public void onClick(View v) {
// final Button button;
button=(Button) v;
String buttonText=button.getText().toString();
enter code here
//Button is set red if answer is correct..
if (curQues.getAnsw().equals(buttonText))
{
button.setBackgroundColor(Color.GREEN);
myscore++;
}
//Button will be set to red if answer is false
else
{
button.setBackgroundColor(Color.RED);
}
if(quid>19){
countDownTimer.cancel();
timerHasStarted = false;
Intent intent = new Intent(TrueFalseActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", myscore); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
finish();
}
}
}
正在开发测验应用程序,它有2个按钮,其中一个onClick()。我点击了一个按钮然后单击下一个相同的颜色正在进行到下一个按钮如何 单击下一步按钮时清除按钮颜色。 我应该如何清除下一个问题的颜色?我是android的新手。任何人都可以帮助我解决这个问题。提前谢谢。
答案 0 :(得分:0)
在onClickListener
中,您的下一个按钮需要重置按钮的颜色;
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
b1.setBackgroundResource(android.R.drawable.btn_default);
b2.setBackgroundResource(android.R.drawable.btn_default);
}
});