我已经创建了一个小测验但是我不知道如何使用Parse实现捕获的按钮来检查答案

时间:2015-03-02 21:18:02

标签: java android parse-platform

我有四个按钮和一个textView。使用以下代码从Parse.com检索textView中的文本:

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();
            }
        }
    });

以下是按钮的代码:

 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);
        }


    });

我希望能够捕获按下的按钮并在之后显示结果屏幕。如何做到这一点,任何指针?

感谢。

1 个答案:

答案 0 :(得分:0)

您不需要所有onClickListeners。您只需要一个RadioGroup,其中包含多个RadioButtons。您必须将它包含在XML布局中,如下所示:

 <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>

然后,在Activity中,您必须将Buttons更改为RadioButtons

然后添加方法onRadioButtonClicked(),如果选择任何RadioButtons,将调用该方法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); }

Activity

根据您的实施,即点击RadioButton后启动新的radioGroup1.getCheckedRadioButtonId(),这是一种很好的方法。

还有许多其他方法,例如使用RadioButton来获取所选的{{1}} ID。

无论如何,您可以在我之前评论中提供的网址中了解更多内容。