按下RadioButton后,通过TextView显示字符串中的文本

时间:2017-03-03 17:56:41

标签: java android

我正试图在按下单选按钮后立即自动显示“这是正确答案”“再试一次”

我的问题是:

如何添加两个字符串

    <string name="Good_answer">That is the correct answer</string>
<string name="Wrong_answer">Try again</string>

到这个textView

<TextView
    android:id="@+id/textViewAnswer"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textAlignment="center"
    android:layout_below="@id/rg2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

所以我可以继续这个

                for (boolean radioAnswer : answer)
                    correct = correct && radioAnswer;
                if (correct)
                    tv.setText(R.string.Good_answer);
                else
                    tv.setText(R.string.Wrong_answer);

这里是setOnClick ...(起初是使用checkbutton 'mbuton'

mbuton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean check = true;
            boolean correct = true;
            // To check if all questions have been answered
            for (boolean radioChecked : checked)
                check = check && radioChecked;
            if (check) {

                // To check if all questions have been answered correctly
                for (boolean radioAnswer : answer)
                    correct = correct && radioAnswer;
                if (correct)
                    tv.setText(R.string.Good_answer);
                else
                    tv.setText(R.string.Wrong_answer);

            }
            else
                tv.setText("Answer all questions");
        }
    });

Xml

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <RadioGroup
        android:id="@+id/rg1"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:textColor="@android:color/black"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Question 1"/>

        <RadioButton
            android:text="Correct Option"
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


        <RadioButton
            android:text="Wrong Option"
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioButton
            android:text="Wrong Option"
            android:id="@+id/radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </RadioGroup>

    <RadioGroup
        android:layout_marginTop="16dp"
        android:layout_below="@+id/rg1"
        android:id="@+id/rg2"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:textColor="@android:color/black"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Question 2"/>


        <RadioButton
            android:text="Wrong Option"
            android:id="@+id/radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioButton
            android:text="Correct Option"
            android:id="@+id/radioButton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


        <RadioButton
            android:text="Wrong Option"
            android:id="@+id/radioButton6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RadioGroup>

    <TextView
        android:id="@+id/textViewAnswer"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textAlignment="center"
        android:layout_below="@id/rg2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

和字符串

   <string name="Good_answer">That is the correct answer</string>
<string name="Wrong_answer">Try again</string>

4 个答案:

答案 0 :(得分:1)

    // i have modified your code, please check it. 
    // i have display message if user does not select any radio button,
    //another wise it display correct answer count on textview.

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.ArrayList;


public class Main2Activity2 extends Activity {

    TextView tv;
    Button mbuton;
    RadioGroup rg1,rg2;
    ArrayList<Integer> arrayListOfRadioGroupId =new ArrayList<Integer>();
    int noAnswerCount= 0;
    int correctAnswerRadioButtonCount= 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.textViewAnswer);
        mbuton = (Button) findViewById(R.id.mbuton);
        rg1 = (RadioGroup)findViewById(R.id.rg1);
        rg2 = (RadioGroup)findViewById(R.id.rg2);

        // Store Radio group id to arraylist
        arrayListOfRadioGroupId.add(rg1.getId());
        arrayListOfRadioGroupId.add(rg2.getId());

        mbuton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                noAnswerCount= 0;
                correctAnswerRadioButtonCount= 0;

                for(int radioGroupId: arrayListOfRadioGroupId)
                {
                    RadioGroup objRadioGroup = (RadioGroup) findViewById(radioGroupId);
                    int checkedId=objRadioGroup.getCheckedRadioButtonId();
                    // get Selected Radio button id.

                    if(checkedId>-1) {
                        RadioButton rB = (RadioButton) findViewById(checkedId);
                        String strRadioButtonText = rB.getText().toString();
                        // get Selected Radio button Text.
                        if(strRadioButtonText.equals("Correct Option"))
                        {
                            correctAnswerRadioButtonCount ++;
                            noAnswerCount --;
                        }
                    }
                    else
                    {
                        noAnswerCount++;
                    }
                }

                if(noAnswerCount > 0)
                {
                    tv.setText("Answer all questions");
                }
                else
                {
                    tv.setText("Correct Answer Count is: " +correctAnswerRadioButtonCount);
                }
            }
        });

        rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                if (checkedId == R.id.radioButton5) {
                    RadioButton rB = (RadioButton) findViewById(checkedId);

                    String strRadioButtonText = rB.getText().toString();
                    // get Selected Radio button Text.
                    if(strRadioButtonText.equals("Correct Option")) {
                        tv.setText("Correct Answer");
                    }
                    else
                    {
                        tv.setText("Wrong Answer");
                    }
                }

            }
        });
    }
}


        // my Xml code is.
        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                            android:id="@+id/activity_main"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:paddingBottom="@dimen/activity_vertical_margin"
                            android:paddingLeft="@dimen/activity_horizontal_margin"
                            android:paddingRight="@dimen/activity_horizontal_margin"
                            android:paddingTop="@dimen/activity_vertical_margin">

                <RadioGroup
                    android:id="@+id/rg1"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:textColor="@android:color/black"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Question 1"/>

                    <RadioButton
                        android:text="Correct Option"
                        android:id="@+id/radioButton1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />


                    <RadioButton
                        android:text="Wrong Option"
                        android:id="@+id/radioButton2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <RadioButton
                        android:text="Wrong Option"
                        android:id="@+id/radioButton3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                </RadioGroup>

                <RadioGroup
                    android:layout_marginTop="16dp"
                    android:layout_below="@+id/rg1"
                    android:id="@+id/rg2"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:textColor="@android:color/black"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Question 2"/>


                    <RadioButton
                        android:text="Wrong Option"
                        android:id="@+id/radioButton4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <RadioButton
                        android:text="Correct Option"
                        android:id="@+id/radioButton5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />


                    <RadioButton
                        android:text="Wrong Option"
                        android:id="@+id/radioButton6"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
                </RadioGroup>

                <TextView
                    android:id="@+id/textViewAnswer"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textAlignment="center"
                    android:layout_below="@id/rg2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            <Button
                android:id="@+id/mbuton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="MButton"
                android:layout_below="@id/textViewAnswer"/>
        </RelativeLayout>

答案 1 :(得分:0)

如果你的问题只是在TextView中设置这些字符串,那么试试这个:

//For correct answer.
String CorrectAnswer = getString(R.string.Good_answer);
tv.setText(CorrectAnswer);

//For wrong answer.
String WrongAnswer = getString(R.string.Wrong_answer);
tv.setText(WrongAnswer);

答案 2 :(得分:0)

守则仍不清楚(我无法弄清楚mButton是什么)。 但是你也可以分享你的布局xml我建议你有一个无线电组包含你想要的X个无线电按钮(选项)并放在checkChangeListener上

#include <stdio.h>
#include <libtar.h>
#include <fcntl.h>

int main(void)
{
   TAR *pTar;
   char *prefix = ".";
   char *filename = "file.tar";

   if ((tar_open(&pTar, filename, NULL, O_WRONLY, 0644, TAR_GNU)) == -1)
     perror("Error1");
   else if ((tar_extract_all(pTar, prefix)) == -1)
     perror("Error2");
   else if ((tar_close(pTar)) == -1)
     perror("Error3");
}

您的活动应实施<RadioGroup android:id="@+id/rgroup" android:layout_width="fill_parent" android:layout_height="45dp" android:background="@drawable/background" android:gravity="center" android:orientation="horizontal" > <RadioButton android:id="@+id/option1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:background="@drawable/test_image" android:button="@null" /> <RadioButton android:id="@+id/option2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:background="@drawable/test_image" android:button="@null" /> </RadioGroup> ,然后您可以定义您的广播组OnCheckedChangeListener

然后,您可以在活动中实现一个方法,如果单击该单选按钮内的选项,则可以使用以下方法。

RadioGroup Options = (RadioGroup) findViewById(R.id.rgroup);

}

假设您可能在动态位置有答案,您可能希望在on checked checked方法中编写一些逻辑。希望这有帮助

答案 3 :(得分:0)

布局文件;看看那些问题和答案。我在这里硬编码了字符串。您可以在字符串资源文件中设置它并使用=SUMIFS(B:B,A:A,">="&$D$1,A:A,"<="&$D$2) 在此处获取它,或使用@string方法动态设置它。

yourTextView.setText()

在你的活动中。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<RadioGroup
    android:id="@+id/rg1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="What is your name?"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/black" />

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Melisa" />


    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Alisha" />

    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Lolita" />

</RadioGroup>

<RadioGroup
    android:id="@+id/rg2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/rg1"
    android:layout_marginTop="16dp"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="What are you learning?"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/black" />


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

    <RadioButton
        android:id="@+id/radioButton5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android" />


    <RadioButton
        android:id="@+id/radioButton6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="HTML" />
</RadioGroup>

<TextView
    android:id="@+id/textViewAnswer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/rg2"
    android:textAlignment="center"
    android:textAppearance="?android:attr/textAppearanceLarge" />

我没有在这里使用字符串资源,如果你想用资源中的字符串设置文本视图,请在我的第一个答案中查看。 所以,我认为这会对你有帮助。