Java 按单选按钮后通过文本视图显示字符串中的文本

Java 按单选按钮后通过文本视图显示字符串中的文本,java,android,Java,Android,我试图在按下单选按钮后自动显示“这是正确答案”或“再试一次” 我的问题是: 如何添加两个字符串 <string name="Good_answer">That is the correct answer</string> <string name="Wrong_answer">Try again</string> 这是setOnClick。。。(一开始是用“mbuton”复选按钮) Xml 和字符串 <string nam

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

我的问题是:

如何添加两个字符串

    <string name="Good_answer">That is the correct answer</string>
<string name="Wrong_answer">Try again</string>
这是setOnClick。。。(一开始是用“mbuton”复选按钮)

Xml


和字符串

   <string name="Good_answer">That is the correct answer</string>
<string name="Wrong_answer">Try again</string>
这是正确的答案
再试一次

如果您的问题只是在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);
代码仍然不清楚(我不知道mButton是什么)。 但是,您也可以共享您的布局xml吗?我建议您创建一个由X个您喜欢的单选按钮(选项)组成的单选组,并将其置于checkedChangeListener上

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


假设您可能在动态位置有答案,您可能希望在on checked change方法中编写一些逻辑。希望这对布局文件有帮助;看看这些问题和答案。我已经在这里硬编码了字符串。您可以在字符串资源文件中设置它,并使用
@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" />

在你的活动中

public class MainActivity extends AppCompatActivity {

private RadioGroup radioGroupFirst, radioGroupSecond;
private String[] realAnswers = {"Melisa", "Android"};
private String[] userAnswers;
private TextView tv;
private boolean status = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    radioGroupFirst = (RadioGroup) findViewById(R.id.rg1);
    radioGroupSecond = (RadioGroup) findViewById(R.id.rg2);
    tv = (TextView) findViewById(R.id.textViewAnswer);
    userAnswers = new String[2];

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

            tv.setText("");
            RadioButton nameRadio = (RadioButton) findViewById(checkedId);
            Log.e("ID", "" + nameRadio.getText().toString());
            userAnswers[0] = nameRadio.getText().toString();
        }
    });

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

            RadioButton learningRadio = (RadioButton) findViewById(checkedId);
            Log.e("ID", "" + learningRadio.getText().toString());
            userAnswers[1] = learningRadio.getText().toString();
            if (checkStatus(userAnswers))
                tv.setText(getString(R.string.Good_answer));
            else
                tv.setText(getString(R.string.Wrong_answer));

        }
    });
}

private boolean checkStatus(String[] userAnswers) {
    status = true;
    for (int i = 0; i < userAnswers.length; i++) {
        Log.e(userAnswers[i], realAnswers[i]);
        if (!userAnswers[i].equals(realAnswers[i]))
            status = false;
    }
    return status;
  }
}
public类MainActivity扩展了AppCompatActivity{
private RadioGroup RadioGroup First,RadioGroup Second;
私有字符串[]realsanswers={“Melisa”,“Android”};
私有字符串[]userAnswers;
私家图文电视;
私有布尔状态=true;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup First=(RadioGroup)findViewById(R.id.rg1);
radioGroupSecond=(RadioGroup)findViewById(R.id.rg2);
tv=(TextView)findViewById(R.id.textViewAnswer);
userAnswers=新字符串[2];
RadioGroup First.setOnCheckedChangeListener(新的RadioGroup.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(RadioGroup组,int checkedId){
tv.setText(“”);
RadioButton名称Radio=(RadioButton)findViewById(checkedId);
Log.e(“ID”,“”+nameRadio.getText().toString());
userAnswers[0]=nameRadio.getText().toString();
}
});
RadioGroup Second.setOnCheckedChangeListener(新的RadioGroup.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(RadioGroup组,int checkedId){
RadioButton learningRadio=(RadioButton)findViewById(checkedId);
Log.e(“ID”,“”+learningRadio.getText().toString());
userAnswers[1]=learningRadio.getText().toString();
如果(检查状态(用户应答))
tv.setText(getString(R.string.Good_response));
其他的
tv.setText(getString(R.string.error_answer));
}
});
}
私有布尔检查状态(字符串[]userAnswers){
状态=真;
for(int i=0;i
我还没有在这里使用字符串资源,如果您想用资源中的字符串设置文本视图,请在我的第一个答案中查看。 所以,我想这会对你有所帮助。

//我已经修改了你的代码,请检查一下。
    // 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>
//如果用户未选择任何单选按钮,我将显示消息, //另一个明智的做法是在textview上显示正确的答案计数。 导入android.app.Activity; 导入android.os.Bundle; 导入android.view.view; 导入android.widget.Button; 导入android.widget.RadioButton; 导入android.widget.RadioGroup; 导入android.widget.TextView; 导入java.util.ArrayList; 公共类Main2活动2扩展活动{ 文本视图电视; 按钮按钮; 放射组rg1、rg2; ArrayList ArrayListOradioGroupId=新的ArrayList(); int noAnswerCount=0; int correctAnswerRadioButtonCount=0; @凌驾 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv=(TextView)findViewById(R.id.textViewAnswer); mbuton=(按钮)findViewById(R.id.mbuton); rg1=(放射组)findViewById(R.id.rg1); rg2=(放射组)findViewById(R.id.rg2); //将无线电组id存储到arraylist arrayListOfRadioGroupId.add(rg1.getId()); arrayListOfRadioGroupId.add(rg2.getId()); mbuton.setOnClickListener(新视图.OnClickListener(){ @凌驾 公共void onClick(视图v){ noAnswerCount=0; correctAnswerRadioButtonCount=0; for(int-radiogroup-id:arrayListOfRadioGroupId) { 放射组objRadioGroup=(放射组)findViewById(放射组ID); int checkedId=objRadioGroup.getCheckedRadioButtonId(); //获取所选单选按钮id。 如果(检查EDID>-1){ RadioButton rB=(RadioButton)findViewById(checkedId); 字符串strRadioButtonText=rB.getText().toString(); //获取所选单选按钮文本。 if(strradiobuttonext.equals(“正确选项”)) { correctAnswerRadioButtonCount++; 诺安斯威尔伯爵——; } } 其他的 { noAnswerCount++; } } 如果(无应答计数>0) { tv.setText(“回答所有问题”); } 其他的 { tv.setText(“更正A
<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>
 @Override
public void onCheckedChanged(RadioGroup group,
                             int checkedId)
{
    switch (checkedId)
    {
        case R.id.option1:
           // settextview to correct here
            break;
        case R.id.option2:
           //set textview to wrong here
            break;

        default:
            break;
    }
<?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" />
public class MainActivity extends AppCompatActivity {

private RadioGroup radioGroupFirst, radioGroupSecond;
private String[] realAnswers = {"Melisa", "Android"};
private String[] userAnswers;
private TextView tv;
private boolean status = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    radioGroupFirst = (RadioGroup) findViewById(R.id.rg1);
    radioGroupSecond = (RadioGroup) findViewById(R.id.rg2);
    tv = (TextView) findViewById(R.id.textViewAnswer);
    userAnswers = new String[2];

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

            tv.setText("");
            RadioButton nameRadio = (RadioButton) findViewById(checkedId);
            Log.e("ID", "" + nameRadio.getText().toString());
            userAnswers[0] = nameRadio.getText().toString();
        }
    });

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

            RadioButton learningRadio = (RadioButton) findViewById(checkedId);
            Log.e("ID", "" + learningRadio.getText().toString());
            userAnswers[1] = learningRadio.getText().toString();
            if (checkStatus(userAnswers))
                tv.setText(getString(R.string.Good_answer));
            else
                tv.setText(getString(R.string.Wrong_answer));

        }
    });
}

private boolean checkStatus(String[] userAnswers) {
    status = true;
    for (int i = 0; i < userAnswers.length; i++) {
        Log.e(userAnswers[i], realAnswers[i]);
        if (!userAnswers[i].equals(realAnswers[i]))
            status = false;
    }
    return status;
  }
}
    // 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>