2013-06-05 106 views
0

我在android上做了一些烹饪应用程序,里面有任何测验,因为我已经能够从数组中获取文本到文本字段的问题,但对于我不知道如何显示它到单选按钮的答案。我是Android新手,请提前帮助我并感谢您。如何将数组中的文本转换为单选按钮?

cuisine1.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="30dp" 
     android:layout_marginTop="20dp" 
     android:src="@drawable/papeda128" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="40dp" 
     android:layout_marginTop="40dp" 
     android:text="Papeda" 
     android:textColor="#000000" 
     android:textSize="30dp" /> 
</LinearLayout> 

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="30dp" > 

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

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp" 
      android:layout_marginTop="30dp" 
      android:text="Question" 
      android:textColor="#000000" 
      android:textSize="20dp" /> 

    </LinearLayout> 
</ScrollView> 

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

    <RadioButton 
     android:id="@+id/radio0" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:checked="true" 
     android:text="Answer 1" /> 

    <RadioButton 
     android:id="@+id/radio1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Answer 2" /> 

    <RadioButton 
     android:id="@+id/radio2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Answer 3" /> 
</RadioGroup> 

</LinearLayout> 

cuisine1.java

package com.culinary; 

import java.util.ArrayList; 
import java.util.Collections; 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.widget.RadioGroup; 
import android.widget.TextView; 


public class cuisine1 extends Activity { 

TextView papquest; 
private int i=0;; 
question q=new question(); 

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

    papquest=(TextView)findViewById(R.id.textView2); 
    String dt=q.showquestionpapeda(i); 
    String[] quest=dt.split("/"); 
    papquest.setText(quest[0],null); 


    findViewById(R.id.radio0).setOnClickListener(mClickListener); 
    findViewById(R.id.radio1).setOnClickListener(mClickListener); 
    findViewById(R.id.radio2).setOnClickListener(mClickListener); 

} 

    RadioGroup.OnClickListener mClickListener = new RadioGroup.OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      i+=1; 

      String dt=q.showquestionpapeda(i); 
      String[] quest=dt.split("/"); 
      papquest.setText(quest[0],null); 

      } 

    }; 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
    } 

} 

question.java

package com.culinary; 

class question { 
    String[] papeda={ 
      "Question 1 ?/Answer1,Answer2,Answer3", 
      "Question 2 ?/Answer1,Answer2,Answer3", 
      "Question 3 ?/Answer1,Answer2,Answer3", 
      "Question 4 ?/Answer1,Answer2,Answer3", 

    }; 

public void question(){ 

    } 

public String showquestionpapeda(int i){ 
     String question; 
     question=papeda[i]; 
     return question; 
    } 
} 

回答

0

你可以做的FOL正在降低:

//Get your string 
String dt=q.showquestionpapeda(i); 
// First split on basis of '/' 
String [] firstSplit = dt.split("/"); 
// Get the second element consisting of "Answer1,Answer2,Answer3" string and split based on "," 
String [] secondSplit = firstSplit[1].split(","); 

//get the radio group and set the text of first second and third radio button 
RadioGroup rbtnGrp = (RadioGroup)findViewById(R.id.radioGroup1); 
for (int i = 0; i < rbtnGrp.getChildCount(); i++) { 
    // setting the text on the three children with three strings in the array 
    ((RadioButton) rbtnGrp.getChildAt(i)).setText(secondSplit[i]); 
} 

您还可以分别设置每个文本的外部循环。希望这可以帮助。

0

你必须做类似如下:

// this get the number of radioButtons inside the radioGroup 
int count = radioGroup.getChildCount(); 
// this is what you are doing to split your quesitons 
String[] quest=dt.split("/"); 

//here we loop through all the child radioButtons 
    for (int i=0;i<count;i++) { 
//get the radio button at the index i 
     View o = radioGroup.getChildAt(i); 
//if the view is a RadioButton 
     if (o instanceof RadioButton) { 
      o.setText(quest[i]); 
     } 
    } 

,并请给我回somefeed

希望有所帮助。

相关问题