简体   繁体   中英

I've created a quiz however I'm not sure how to implement the capturing of the buttons pressed to check against the answer using Parse

I've got four buttons and a textView. The text in the textView is retrieved from Parse.com using the following code:

final ParseQuery<ParseObject> quizOne = ParseQuery.getQuery("Quiz");
    quizOne.getFirstInBackground(new GetCallback<ParseObject>() {

        public void done(ParseObject reqDetails, ParseException e) {

            if (quizOne != null) {
                Log.d("quizOne", "Got it");
                //Retrieve Age
                String gettheQuestion = reqDetails.getString("questionOne");
                TextView getQone = (TextView) findViewById(R.id.quesOne);
                getQone.setText(gettheQuestion);
                Toast.makeText(getApplicationContext(),
                        "Successfully Recieved Question",
                        Toast.LENGTH_LONG).show();


            } else {
                Log.d("quizOne", "Question not retreived.");
                Toast.makeText(getApplicationContext(),
                        "Can't Get Details, Check Connection", Toast.LENGTH_LONG)
                        .show();
            }
        }
    });

and here is the code for the buttons:

 optionAq.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (optionAq.equals(optionAq)) {
                Intent intent = new Intent(Quiz.this, Question2.class);
                startActivity(intent);
            }
        }
    });

    optionBq.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Quiz.this, Question2.class);
            startActivity(intent);
        }


    });

    optionCq.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Quiz.this, Question2.class);
            startActivity(intent);
        }


    });

    optionDq.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Quiz.this, Question2.class);
            startActivity(intent);
        }


    });

I want to be able to capture the buttons pressed and show like a result screen after. How would I go by doing this, any pointers?

Thanks.

You don't need all those onClickListeners . You just need a RadioGroup with multiple RadioButtons inside it. You have to include it in your XML layout, like this:

 <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView1"
        android:orientation="vertical" >

        <RadioButton
            android:id="@+id/optionAq"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked"
            android:text="A" />

        <RadioButton
            android:id="@+id/optionAb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked"
            android:text="B" />

        <RadioButton
            android:id="@+id/optionCb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked"
            android:text="C" />

        <RadioButton
            android:id="@+id/optionDb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked"
            android:text="D" />
    </RadioGroup>

Then, in your Activity , you have to change your Buttons to RadioButtons .

Then add the method onRadioButtonClicked() that will be invoked if any of the RadioButtons is selected.

public void onRadioButtonClicked(View v){
        Log.i("Answer", "Answer is "+ ((RadioButton)v).getText());
        Intent intent = new Intent(Quiz.this, Question2.class);
        //send the answer to the next Activity if yo need
        intent.putExtra("answer", ((RadioButton)v).getText());
        //start next Activity
        startActivity(intent);
    }

According to your implementation, that is launching a new Activity once a RadioButton is clicked, this is a good approach.

There are many other approaches, like using radioGroup1.getCheckedRadioButtonId() to get the selected RadioButton id.

Anyway, you can learn this and more in the URL I provided in my previous comment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM