-1
I am developing a quiz. Using as Radiobutton response. So far I have created using the onClick method on each Radiobutton, but it is not effective.
So I’d like to call every question ask1();
, ask2();
within the method rate(View view)
and from there calculate the total score of the user’s responses showing a Toast.
But when calling for example the method ask6();
within the rate(View view);
error. Even if I put the view parameter ask(view);
why rate has is a view.
public class MainActivity extends AppCompatActivity {
double rating = 0;
String userAnswer;
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void ask1( View view ) {
//See if checked
boolean checked = ((RadioButton) view).isChecked();
//Do some if checked
switch (view.getId()) {
case R.id.answer1_ask1:
if (checked) {
rating += 1;
break;
}
default:
if (checked) {
rating -= 1;
break;
}
}
}
public void ask2( View view ) {
//is checked?
boolean checked = ((CheckBox) view).isChecked();
//Do some
switch (view.getId()) {
case R.id.answer1_ask2:
if (checked) rating -= 1;
case R.id.answer2_ask2:
if (checked) rating += 0.5;
case R.id.answer3_ask2:
if (checked) rating += 0.5;
}
}
public void ask3(){
EditText answer = findViewById(R.id.answer1_ask3);
userAnswer = String.valueOf(answer.getText());
if (userAnswer.equals("Láctea")) {
rating += 1;
} else if (userAnswer.equals("Via Láctea")) {
rating += 1;
} else {
rating -= 1;
}
}
public void ask4(View view){
//See if checked
boolean checked = ((RadioButton) view).isChecked();
//Do!
switch (view.getId()) {
case R.id.answer1_ask4:
if (checked) {
rating += 1;
break;
}
case R.id.answer2_ask4:
if (checked) {
rating -= 1;
break;
}
case R.id.answer3_ask4:
if (checked) {
rating -= 1;
break;
}
}
}
public void ask5(View view){
//is checked?
boolean checked = ((RadioButton)view).isChecked();
//Do it now!!!!
switch (view.getId()){
case R.id.answer1_ask5:
if(checked){
rating += 1;
}
case R.id.answer2_ask5:
if (checked) {
rating -= 1;
}
}
}
public void ask6(View view){
//is checked?
boolean checked = ((RadioButton)view).isChecked();
//Please do fast!
switch (view.getId()){
case R.id.answer1_ask6:
if(checked){
rating += 1;
}
case R.id.answer2_ask6:
if(checked){
rating -= 1;
}
}
}
public void rate( View view ) {
// ask6(View view); Why can't I call?
// ask6(view); Why bug?
ask6();
Toast.makeText(this, ("Oi "+ rating ), Toast.LENGTH_SHORT).show();
rating = 0;
}
/*
public void ask3( View view ) {
//was wrote?
//String answer = ((EditText)view).getText(findViewById(R.id.answer1_ask3)).toString();
//was wrote?
EditText answer = findViewById(R.id.answer1_ask3);
String userAnswer = String.valueOf(answer.getText());
String rightAnswer1 = "Via Láctea";
String rightAnswer2 = "Láctea";
//do some
if(userAnswer == rightAnswer1) rating += 1;
if (userAnswer == rightAnswer2) rating += 1;
else rating -= 1;
// if (userAnswer == "a"){
}
*/
}
Part of XML:
<RadioGroup
android:layout_marginLeft="72dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp">
<RadioButton
android:id="@+id/answer1_ask6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/answer1_ask6"/>
<RadioButton
android:id="@+id/answer2_ask6"
android:layout_marginVertical="4dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/answer2_ask6"/>
</RadioGroup>
<Button
android:text="RESPONDER"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="rate"
android:layout_marginLeft="72dp"
android:layout_marginBottom="20dp"/>
</LinearLayout>
=================== Adding an edit =============
In the Android documentation, there is an example of how to use Radiobutton:
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_pirates:
if (checked)
// Pirates are the best
break;
case R.id.radio_ninjas:
if (checked)
// Ninjas rule
break;
}
}
The good thing about this method is that I just have to play the id of each Radiobutton and use the switch to run everything at once. Then I edited my code to make case of each Radiobutton I have in the quiz. It was like this:
public void askRadioButton(View view){
//is checked?
boolean checked = ((RadioButton)view).isChecked();
//Do it now!!!!
switch (view.getId()){
case R.id.answer1_ask1:
if (checked) {
rating += 1;
break;
}
case R.id.answer1_ask2:
if (checked) rating -= 1;
break;
case R.id.answer2_ask2:
if (checked) rating += 0.5;
break;
case R.id.answer3_ask2:
if (checked) rating += 0.5;
break;
case R.id.answer1_ask4:
if (checked) {
rating += 1;
break;
}
case R.id.answer2_ask4:
if (checked) {
rating -= 1;
break;
}
case R.id.answer3_ask4:
if (checked) {
rating -= 1;
break;
}
case R.id.answer1_ask5:
if(checked){
rating += 1;
}
case R.id.answer2_ask5:
if (checked) {
rating -= 1;
}
}
}
public void rate( View view ) {
askRadioButton();
Toast.makeText(this, ("Oi "+ rating ), Toast.LENGTH_SHORT).show();
rating = 0;
}
But I still can’t properly call the new method. If I remove the view parameter, the rest of the code doesn’t work.
You are not passing the parameter
view
when calling the methodask6();
– Valdeir Psr
I already tried to pass the view parameter, but of the error. Why rate is a method of a button of my xml @Valdeirpsr
– Nakamoto
I get it. Then replace
((RadioButton)view).isChecked();
for((RadioButton) findViewById(R.id.ID_DO_RADIO_BUTTON)).isChecked();
and pass the parameterview
– Valdeir Psr
Can you provide the classes of each method? And also what objects do you have that instance?
– Jefferson Quesado
Hi @Valdeirpsr I described the problem better. Can you look please?
– Nakamoto
@Jeffersonquesado I described the problem better, you could take a look?
– Nakamoto