【问题标题】:How to get selected radio button value from question1 activity page to next page如何从 question1 活动页面获取选定的单选按钮值到下一页
【发布时间】:2015-01-21 03:08:39
【问题描述】:

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

问题1。爪哇

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。爪哇

  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 中检索值并将其显示在问题 2 页面上。

【问题讨论】:

    标签: android eclipse radio-button


    【解决方案1】:

    你可以使用:

    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 对象的引用

    【讨论】:

    • 我收到此错误“- 操作数类型不兼容的 RadioButton 和 String”
    • 如果要测试RadioButton是否被选中,可以使用r1.isChecdked()。您不应该尝试某种字符串转换并针对字符串进行测试。只需使用已经存在的布尔值。
    • 如何获取下一页的价值?
    【解决方案2】:

    在您的第二个活动中,从 Intent Extras 中获取值:

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

    【讨论】:

    • 我已尝试使用此代码。但是在我在模拟器上运行应用程序后,它出现了一个错误,无法转到下一页
    猜你喜欢
    • 2014-10-27
    • 2021-01-19
    • 2013-12-15
    • 2013-12-14
    • 1970-01-01
    • 2023-03-17
    • 2016-02-01
    • 2020-11-16
    • 1970-01-01
    相关资源
    最近更新 更多