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

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

最佳答案

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

关于java - 按下 RadioButton 后通过 TextView 显示字符串中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42585516/

相关文章:

java - 更改 apache Camel 的 http 响应和正文响应

java - 在应用计费和替换公钥中。这有什么大不了的?

java - 在 Android Studio 中应用于 URL 末尾时出现 "null"EditText 错误

android - 如何在 Android 中每 5 分钟刷新一次文件读取?

android - 尝试为具有挂起请求的 session 请求新权限

android - 抽屉导航 fragment 不断返回 null

eclipse - Ubuntu Eclipse 编译器 1.8 不出现

java - 自动绑定(bind)复杂 (JSON) 表单数据

java - 如何在 Java 中解析 JSON 对象的精确值?

java - 计算在 android 2.2 的日历 View 中显示的第一天