I am learning how to code this game and I have noticed that once the answer entered is correct one can click on the answer and still add on the score. I was wondering how can i make sure that the answer is only entered once? if the answer is true for someone to get 1 score?
if(answer == q.getAnswer()){
scoreTxt.setText("Score: "+(putScore+1));
correct = true;
}else if(answer != q.getAnswer()){
setHighScore();
scoreTxt.setText("Score: 0");
There are several options. One example is disabling the button after it has been clicked in the OnClickListener:
button.setEnabled(false);
Don't forget to enable the button once moving on to the next question (I'm assuming your game has questions and answers).
You're marking a bool as true. Why not use it to make sure the check can only succeed once?
if(answer == q.getAnswer() && !correct) {
Related
I'm trying to make an app that has a next and back button that shows different photos, my next button works but the back button does not. I used someone's code I found online for the next button (theirs was just a "change button" button) and altered their code to just be a next button instead. Then tried altering the next button to just do the opposite for the back button but I'm running into problems. I apologize if this is a simple question, I'm new to coding in general and don't have much understanding of most of the concepts. [This is what I've tried](https://i.stack.imgur.com/hRd3K.png)
I tried to make the b2 setOnClickListener just do the opposite of the b1 listener but instead of going back it goes next one photo and then crashes the app. The next button works fine until I click the back button, then the next time I click the next button the app also crashes.
The actual problem is in your indexing. You have 3 images inside your images array. When you press the back button and you check if i == 0 then you set i = 3. There isn't index 3 inside your array since it goes from 0 to 2.
That's when you probably get indexed out of bounds and the app crashes. Try changing
if (i == 0) i = 3;
to
if (i == 0) i = 2;
Also maybe do your operations on 'i' before getting image resource. Like this:
b1!!.setOnClickListener{
++i;
if (i == 3) i = 0;
iv!!.setImageResource(images[i]);
}
b1!!.setOnClickListener{
--i;
if (i == 0) i = 2;
iv!!.setImageResource(images[i]);
}
That's why when you press the back button you don't go back but instead go forward. Maybe even that causes the index out of bounds since you actually always save the future index in i and not the current index.
i want create a key pressed (hold key) func for my application.
for example when I press (hold) a button next year the values of textview grow and when I pickup my finger from button, stop working.
anyone can help me i most use which of Listener or function for create it?
this is my setOnClickListener but i want button press(hold) work
nextDay.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (values >= downrange && values <= uprange)
values++;
if (values > uprange)
values = downrange;
if(values <10)
textDayo.setText(PersianReshape.reshape("0"+String.valueOf(values)));
else
textDayo.setText(PersianReshape.reshape(String.valueOf(values)));
}
});
thanks for your help
and sorry for bad english
when I press (hold) a button next year the values of textview grow
Have you looked at the DatePicker? This allows you to set a date very easily:
You can follow this tutorial
If you do not want to use a DatePickker, you can try to use an OnTouchListener and a Handler to do this yourself, but this is hard to do.
I understand that you are trying to hold a button (widget) and while holding it to increase the year.
If this is the case then this link should help you. The idea is that you use a timer to update the ui. Something similar is also described here. If your problem is just setting the date though, a simpler way could be the one suggested by Sam above.
I am developing the calculator for android.
I have implemented all the functionality, Now just getting stop at the decimal value.
I dont know how to implement that functionality on the click of custom "." button.
I want decimal value up to two fraction point.
can anybudy help me to develop such code?
I need it. . . I know it seem very easy but right now i got stuck in that matter.
Pleae help me in this
Thanks in advance.
As it is, this is a very broad question. I recommend you to check this http://www.codeproject.com/KB/android/androidcalculator.aspx
Generally speaking, you need to:
1) Implement UI
2) Implement click handlers for the buttons
3) Implement evaluation function
If you get stuck at something particular ask it here and post your code.
EDIT:
There you can see how it is implemented, you can use similar logic:
case DECIMAL_SEP: // Handle decimal seperator
if (hasFinalResult || resetInput) { // if previous result is calculated or the input is reset, insert 0 before
userInputText.setText("0.");
hasFinalResult = false;
resetInput = false;
} else if (currentInput.contains(".")) // don't let 2nd '.'
return;
else
userInputText.append("."); // append '.'
break;
I don't know if this was asked or not (I searched a little but no result) but I'm kinda time pressed and I need help
I've been developing an application in Android and I've only started under this platform for 3 months
I have a choice test with different questions and yes and no answers using radio buttons
I want to count the "yes" answers but even like this if I put r1.isChecked() instead of for and buttons[i].isChecked() it counts the clicks
Here is what I tried until now and I get force close everytime I click on the first radiobutton RadioButton[]buttons={rb1, rb3,rb5} ;
public void onClick(View view){
checkStates(buttons);
}
private void checkStates(RadioButton[] buttons) {
for (int i=0; i<buttons.length; i++) {
if (buttons[i].isChecked())
da++;}
tv.setText("Result:"+yes);
}
How can I tell my appplication that the radiobuttons are already checked and no need to increment it on a second click (on the same radio button?)
Is there a way to count using the ids or the name of the radiobuttons?
If so how should I do it? Some tutorials (I love them) and tips would help a lot.
Thanks.
You wouldn't want to count each click or even each "state change" because that wouldn't be accurate.
The best thing to do would be to create a function called checkStates(RadioButton[] buttons) { }
You would call this function each time the user clicks a RadioButton.
You would have all your RadioButton objects stored in an array, which you pass to the function, and then the function goes through the list of RadioButtons and checks their state. If it counts 6 as being Yes, then go to your other activity.
I'm having an issue with RadioGroup's clearChecked(). I'm displaying a multiple choice question to the user and after the user selects an answer I check the answer, give him some feedback and then move to the next question. In the process of moving to the next question I clearCheck on the RadioGroup.
Can anyone explain to me why the onCheckedChanged method is called 3 times? Once when the change actually occurs (with the user changes), once when I clearCheck(with -1 as the selected id) and once in between (with the user changes again)?
As far as I could tell the second trigger is provoked by clearCheck. Code below:
private void checkAnswer(RadioGroup group, int checkedId){
// this makes sure it doesn't blow up when the check is cleared
// also we don't check the answer when there is no answer
if (checkedId == -1) return;
if (group.getCheckedRadioButtonId() == -1) return;
// check if correct answer
if (checkedId == validAnswerId){
score++;
this.giveFeedBack(feedBackType.GOOD);
} else {
this.giveFeedBack(feedBackType.BAD);
}
// allow for user to see feedback and move to next question
h.postDelayed(this, 800);
}
private void changeToQuestion(int questionNumber){
if (questionNumber >= this.questionSet.size()){
// means we are past the question set
// we're going to the score activity
this.goToScoreActivity();
return;
}
//clearing the check
gr.clearCheck();
// give change the feedback back to question
imgFeedback.setImageResource(R.drawable.question_mark); //OTHER CODE HERE
}
and the run method looks like this
public void run() {
questionNumber++;
changeToQuestion(questionNumber);
}
What I've discovered is that if an item is checked and you call clearCheck() on the radio group it will call onCheckedChanged twice. The first time with the id of the item that was checked and the second time with -1/View.NO_ID. IMHO, this is a bug and apparently it has been around since at least 1.6. See this google code bug report: http://code.google.com/p/android/issues/detail?id=4785
It seems to be that the only solution is to check the actual RadioButton.isChecked() and test if it is true or false. This sort of defeats the purpose of the onCheckedChanged returning the id of the item since you now have to either keep references to those buttons or call findViewById every time.
I doubt they will fix this since changing it would probably break existing code in unexpected ways.
I faced the same problem and i solved with other work-around.
Set CheckedChangeListener as NULL
Do your operation
Reset your OnCheckedChangeListener again
Code snnipet :
rdbGroup.setOnCheckedChangeListener(null);
rdbGroup.clearCheck();
rdbGroup.setOnCheckedChangeListener(checkedChangeListener);
Hope this will help ツ
I had similar problem. My solution:
in procedure:
public void onCheckedChanged(RadioGroup rGroup, int checkedId)
I check checkedId. It equals to -1 if you use clearCheck()
else it equals to selected radiogroup child