2017-06-15 192 views
0

我正在製作一個測驗應用程序,其中有四個單選按鈕。每次點擊「下一步」按鈕時,我想要更改所有四個選項。我應該使用二維數組還是使用任何其他邏輯。請通過編碼給出適當的邏輯。每次單擊按鈕時更改多個單選按鈕文本:android

Layout file

Java code

+0

請張貼您的代碼。 – GGWP

+0

是你的數據從數據庫或其靜態? – sajan

+0

我已經將值存儲在字符串文件中。不涉及數據庫 –

回答

0

您可以Butterknife

@BindViews({ R.id.radio1, R.id.radio2, R.id.radio3}) 
List<RadioButton> radioList; 


static final ButterKnife.Action<RadioButton> action = new ButterKnife.Action<RadioButton>() { 
    @Override public void apply(RadioButton rb, int index) { 
    //Do your stuff here 
    } 
}; 


void onNextButton() { 
ButterKnife.apply(radioList, action); 
} 
+0

主要問題是在單選按鈕上應用更改;即:如何選擇字符串值,然後將其設置爲要從中選擇字符串值的單選按鈕 –

+0

的setText屬性? –

0

做嘗試這種模式

佈局

<?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:orientation="vertical" 
android:padding="10dp" > 

<RadioGroup 
    android:id="@+id/radioGroupAnswrs" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" > 

    <RadioButton 
     android:id="@+id/radio0" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:checked="true" 
     android:text="ansOpt1" /> 

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

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

    <RadioButton 
     android:id="@+id/radio3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="ansOpt4" /> 

</RadioGroup> 

<Button 
    android:id="@+id/btnNext" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:text="Next" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/radioGroupAnswrs" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="30dp" 
    android:text="Questin" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

</RelativeLayout> 

String.xml`

<string name="app_name">app name</string> 
<string name="hello_world">Hello world!</string> 
<string name="action_settings">Settings</string> 

<string-array name="qtns"> 
    <item >questian1</item> 
    <item >questian2</item> 
    <item >questian3</item> 
    <item >questian4</item> 
    <item >questian5</item> 
</string-array> 

<string-array name="ans"> 
    <item >#Q1opt1#Q1opt2#Q1opt3#Q1opt4</item> 
    <item >#Q2opt1#Q2opt2#Q2opt3#Q2opt4</item> 
    <item >#Q3opt1#Q3opt2#Q3opt3#Q3opt4</item> 
    <item >#Q4opt1#Q4opt2#Q4opt3#Q4opt4</item> 
    <item >#Q5opt1#Q5opt2#Q5opt3#Q5opt4</item> 
</string-array> 

POJO類

public class Quiz { 

private String question; 
private String anwrs[]; 

public Quiz(String quest, String[] answrs) { 
    this.question = quest; 
    this.anwrs = answrs; 
} 

public String getQuestion(){ 
    return this.question; 
} 

public String[] getAnsOptions(){ 
    return this.anwrs; 
} 

} 

Activity類

public class QuizActivity extends Activity { 

TextView tvQstn; 
RadioButton rbOp1, rbOp2, rbOp3, rbOp4; 
Button btnNext; 

ArrayList<Quiz> aryQuiz; 
int quesCnt = 0; 

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

    aryQuiz = new ArrayList<Quiz>(); 

    initViews(); 

    setArray(); 

    setQuestion(); 

    btnNext.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      setQuestion(); 
     } 
    }); 

} 

private void initViews() { 
    tvQstn = (TextView) findViewById(R.id.textView1); 
    rbOp1 = (RadioButton) findViewById(R.id.radio0); 
    rbOp2 = (RadioButton) findViewById(R.id.radio1); 
    rbOp3 = (RadioButton) findViewById(R.id.radio2); 
    rbOp4 = (RadioButton) findViewById(R.id.radio3); 
    btnNext = (Button) findViewById(R.id.btnNext); 

} 

private void setQuestion() { 
    if (aryQuiz.size() > quesCnt) { 
     Quiz cQuiz = aryQuiz.get(quesCnt); 
     tvQstn.setText(cQuiz.getQuestion()); 
     rbOp1.setText(cQuiz.getAnsOptions()[0]); 
     rbOp2.setText(cQuiz.getAnsOptions()[1]); 
     rbOp3.setText(cQuiz.getAnsOptions()[2]); 
     rbOp4.setText(cQuiz.getAnsOptions()[3]); 
     quesCnt++; 
    } 
} 

private void setArray() { 

    String[] qtns = getResources().getStringArray(R.array.qtns); 
    String[] ans = getResources().getStringArray(R.array.ans); 

    for (int i = 0; i < qtns.length; i++) { 
     Quiz quiz = new Quiz(qtns[i], ans[i].split("#")); 
     aryQuiz.add(quiz); 
    } 

} 

} 
相關問題