我在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 :(得分:0)
您可以执行以下操作:
//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]);
}
您还可以单独为每个循环设置文本外部循环。希望这会有所帮助。
答案 1 :(得分: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]);
}
}
请给我一些回馈
希望有所帮助。