如何从question1活动页面到下一页获取所选单选按钮值

时间:2014-11-23 09:12:19

标签: android eclipse radio-button

我正在使用移动应用程序开发测验。目前,我在如何从上一页检索所选单选按钮时遇到问题。我希望选择的单选按钮值显示在下一页上。下面是我在xml和java中的代码。

问题1。的java

public class question1 extends Activity {
    private Button Button2;
    public RadioButton r1,r2,r3;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.question1); 

        r1 = (RadioButton)findViewById(R.id.rb1);
        r2 = (RadioButton)findViewById(R.id.rb2);
        r3 = (RadioButton)findViewById(R.id.rb3);

        //Button2
        Button2 = (Button) findViewById(R.id.button1);
        Button2.setOnClickListener((new OnClickListener() {                 
            public void onClick(View view) {
                Intent intent = new Intent(view.getContext(),question2.class);
                intent.putExtra("r1",r1.isSelected());
                intent.putExtra("r2",r2.isSelected());
                intent.putExtra("r3",r3.isSelected());
                startActivityForResult(intent,0);               
            }
        }));
    }
} 

这是我在question1.xml中的代码。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="10dp" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="452dp"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:layout_below="@+id/textView2"
                android:layout_centerHorizontal="true"
                android:src="@drawable/clay" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="24dp"
                android:text="YANG MANA SATUKAH DI ANTARA AYAT-AYAT BERIKUT YANG BENAR ?"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <RadioGroup
                android:id="@+id/radioGroup1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <RadioButton
                    android:id="@+id/rb1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/radioButton2"
                    android:text="A.Tanah liat yang leper lebih berat" />

                <RadioButton
                    android:id="@+id/rb2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/radioButton3"
                    android:text=" B. Kedua-dua tanah liat adalah sama berat" />

                <RadioButton
                    android:id="@+id/rb3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/button1"
                    android:layout_alignLeft="@+id/radioButton2"
                    android:layout_marginBottom="14dp"
                    android:text="C.Bola tanah liat lebih berat" />
            </RadioGroup>

            <Button
                android:id="@+id/button1"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="right"
                android:background="@drawable/nextnext" />
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

这是我希望显示所选单选按钮值的第二页的代码。

问题2。的java

  public class question2 extends Activity
  {
  private Button Button2;
  public RadioButton r1,r2,r3,r4,r5;
  public RadioGroup rg1,rg2;
  public String a2,b2,total;

  public TextView output ;



    public void onCreate(Bundle savedInstanceState)
    {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.question2);



   output = (TextView)findViewById(R.id.textView5);
   r1 = (RadioButton)findViewById(R.id.rb1);

   r2 = (RadioButton)findViewById(R.id.rb2);
   r3 = (RadioButton)findViewById(R.id.rb3);

   r4 = (RadioButton)findViewById(R.id.rb4);
   r5 = (RadioButton)findViewById(R.id.rb5);
   rg1 = (RadioGroup)findViewById(R.id.radioGroup1);     
   rg2 = (RadioGroup)findViewById(R.id.radioGroup2);

   Object RadioButton;
   r1= (RadioButton).getValue();
   r2 =(RadioButon).getValue();
   r3 =(RadioButon).getValue();



 //Button2
        Button2 = (Button) findViewById(R.id.button1);
        Button2.setOnClickListener((new OnClickListener()
        {

            public void onClick(View view) 
            {
                Intent intent = new Intent(view.getContext(),question3.class);


                startActivityForResult(intent,0);
            }
        }
            ));

        {
                switch(rg2.getCheckedRadioButtonId()){

                case R.id.rb4:
                    if (r4.isChecked()){
                        //a2 =0;

                    }
                        break;



                case R.id.rb5:
                    if (r5.isChecked()){
                        //b2 = 1;


                    }
                        break;

                       //total = rg1.getCheckedRadioButtonId()+rg2.getCheckedRadioButtonId()  ;    
                }}
        //receive the arguments from the previous Activity
        Bundle extras = getIntent().getExtras();
        if (extras==null) {
            return;
        }

        output = (TextView) findViewById(R.id.textView5);

        Bundle data = this.getIntent().getExtras();

         r1=data.getValue(r1);
         r2=data.getValue(r2);
         r3=data.getValue(r3);

         double result = 0;

        if (r1 == "")
        {
            result+ = 1;
        }
        else if (r2 == "true"){
            result+ = 0;}

        else if (r3=="true")
        {
            result+ = 0;
        }
        else {
            result + =0;
        }

        }
        }

        }

question2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="452dp"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:layout_below="@+id/textView3"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="20dp"
                android:layout_weight="0.13"
                android:src="@drawable/soklan2" />

            <RadioGroup
                android:id="@+id/radioGroup2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/textView4"
                android:layout_below="@+id/textView4" >

                <RadioButton
                    android:id="@+id/rb4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="A. Ya" />

                <RadioButton
                    android:id="@+id/rb5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/radioGroup1"
                    android:layout_below="@+id/radioGroup1"
                    android:text="B. Tidak" />
            </RadioGroup>

            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/radioGroup2"
                android:layout_marginTop="20dp"
                android:text="Result"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <Button
                android:id="@+id/button1"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="right"
                android:background="@drawable/nextnext" />
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

我想知道,如何从问题1中检索值并将其显示在question2页面上。

2 个答案:

答案 0 :(得分:0)

你可以使用:

r1.setChecked(getIntent().getExtras().getBoolean("r1"));
r2.setChecked(getIntent().getExtras().getBoolean("r2"));
r3.setChecked(getIntent().getExtras().getBoolean("r3"));

您可能还需要更改放置值的代码:

intent.putExtra("r1",r1.isChecked());
intent.putExtra("r2",r2.isChecked());
intent.putExtra("r3",r3.isChecked());

你也应该看一下这段代码:

Object RadioButton;
r1 = (RadioButton).getValue();
r2 = (RadioButon).getValue();
r3 = (RadioButon).getValue();

我不知道你在这里想要完成什么。并且应该删除它以保持对RadioButton对象

的反馈

答案 1 :(得分:0)

在第二个活动中,从意图附加内容中获取值:

 Intent intent = getIntent();
 boolean r1Value = intent.getBooleanExtra("r1",false);
 r1.setChecked(r1Value );