2014-11-23 71 views
0

我正在開發使用移動應用程序的測驗。目前,我在如何從上一頁中檢索選定的單選按鈕時遇到問題。我希望所選的單選按鈕值顯示在下一頁上。下面是我在xml和java中的代碼。如何從question1活動頁面中獲取選定的單選按鈕值到下一頁

question1。 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> 

這是我的第二頁的代碼,我希望顯示所選的單選按鈕值。

question2。 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的值,顯示其問題2頁。

回答

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對象

+0

我怎樣才能糾正if-else語句? – 2014-11-23 09:40:56

+0

我得到這個錯誤「 - 不兼容的操作數類型RadioButton和字符串」 – 2014-11-23 09:41:44

+0

如果您想測試是否選中了RadioButton,則可以使用'r1.isChecdked()'。您不應該嘗試某種字符串轉換並針對字符串進行測試。只需使用已經存在的布爾值即可。 – TmKVU 2014-11-23 09:53:00

0

在你第二個活動,從羣衆演員的意圖得到的值:

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

我已經嘗試使用此代碼。但是當我在模擬器上運行應用程序後,它得到一個錯誤,無法進入下一頁 – 2014-11-23 13:24:01

相關問題