简体   繁体   English

我创建了一个测验,但是我不确定如何使用Parse捕获捕获的按钮以检查答案

[英]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. 我有四个按钮和一个textView。 The text in the textView is retrieved from Parse.com using the following code: 使用以下代码从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();
            }
        }
    });

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 . 您不需要所有这些onClickListeners You just need a RadioGroup with multiple RadioButtons inside it. 您只需要一个带有多个RadioButtonsRadioGroup You have to include it in your XML layout, like this: 您必须将其包含在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>

Then, in your Activity , you have to change your Buttons to RadioButtons . 然后,在“ Activity ,必须将“ Buttons更改为“ RadioButtons Buttons

Then add the method onRadioButtonClicked() that will be invoked if any of the RadioButtons is selected. 然后添加方法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);
    }

According to your implementation, that is launching a new Activity once a RadioButton is clicked, this is a good approach. 根据您的实现,即在单击RadioButton启动一个新的Activity ,这是一个很好的方法。

There are many other approaches, like using radioGroup1.getCheckedRadioButtonId() to get the selected RadioButton id. 还有许多其他方法,例如使用radioGroup1.getCheckedRadioButtonId()获取选定的RadioButton ID。

Anyway, you can learn this and more in the URL I provided in my previous comment. 无论如何,您可以在我之前的评论中提供的URL中了解到更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在为测验程序使用JTabbedPane,不确定如何获取按钮以显示答案 - I'm using JTabbedPane for my quiz program, not sure how to get my buttons to display the answers 如何检查用户答案与测验中的代数答案相同? - How Do I Check If User Answer Is Same As Algebra Answer In Quiz? 我正在尝试使用 PDF Box 加载 PDF,但是 PDF 无法打开。 我不确定我的代码有什么问题 - I'm trying to load a PDF using PDF Box however, the PDF just wont open. I'm not sure whats wrong in my code 我已经创建了基本的GUI,但是按钮不会与文本字段交互 - I've created a basic GUI but buttons won't interact with textfield 我如何做才能让我的测验让我回答? - How do I make it so my quiz lets me answer? 我在我的驱动程序类中不断出错,但我不确定我做错了什么 - I keep getting an error in my driver class, but I'm not sure what I've done wrong 如何测试我创建的Web服务? - How to test Web Service I've created? 如何确保我使用“服务器”JVM? - How to make sure I'm using the “server” JVM? 递归方法没有达到基本情况,但是,我不确定为什么。 爪哇 - Recursive method not reaching the base case, however, I'm not sure why. Java 我不确定我的程序正在读取我要求它输入的文件 - I'm not sure my program is reading the file input I've asked it to
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM