[英]How to update the text of radio buttons with strings.xml
我構建了一個應用程序,詢問用戶幾個問題。 每個問題都有4個答案(單選按鈕),用戶需要選擇。 我想構建一個用問題和答案更新的活動。
所有問題和答案都寫在strings.xml
:
<!-- Q1 -->
<string name="Q1">Q1</string>
<string name="Q1A1">answer 1</string>
<string name="Q1A2">answer 2</string>
<string name="Q1A3">answer 3</string>
<string name="Q1A4">answer 4</string>
<!-- Q2 -->
<string name="Q2">Q2</string>
<string name="Q2A1">answer 1</string>
<string name="Q2A2">answer 2</string>
<string name="Q2A3">answer 3</string>
<string name="Q2A4">answer 4</string>
....
(我希望將來可以通過此文件添加更多問題,而不是以硬編碼的方式)
當用戶選擇進入下一個問題時,我想通過動態方式更新問題和答案:
private void updateQuestion(int currentQuestion) {
// Update Questions
TextView textView = (TextView)findViewById(R.id.QuestionLabel);
textView.setText(getResources().getString(R.string.Q1)); // ??? how to get string Q#currentQuestion
// Update Answares
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.rgQuestions);
for (int i = 0; i < radioGroup .getChildCount(); i++)
{
// ??? how to get string of Q'currentQuestion'A'i' ????
((RadioButton) radioGroup.getChildAt(i)).setText(getResources().getString(R.string.Q1A1));
}
}
但是如何通過選擇正確的字符串值來設置通過strings.xml的單選按鈕(和textView)的文本? (我想用currentQuestion和i迭代器構建StringRes)。 可能嗎?
您可以使用String Array:
<string-array name="question_array">
<item>Q1</item>
<item>Q2</item>
</string-array>
<string-array name="question_1_answer">
<item>answer 1</item>
<item>answer 2</item>
<item>answer 3</item>
<item>answer 4</item>
</string-array>
<string-array name="question_2_answer">
<item>answer 1</item>
<item>answer 2</item>
<item>answer 3</item>
<item>answer 4</item>
</string-array>
並通過以下方式訪問:
String[] questions = getResources().getStringArray(R.array.question_array);
//Call as per your convenience
updateQuestion(1, R.array.question_1_answer);
//updateQuestion(2, R.array.question_1_answer);
在你的例子中:
private void updateQuestion(int currentQuestion, int answerResId) {
TextView textView = (TextView)findViewById(R.id.QuestionLabel);
textView.setText(questions[currentQuestion]);
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.rgQuestions);
String[] answers = getResources().getStringArray(answerResId);
for (int i = 0; i < radioGroup .getChildCount(); i++) {
((RadioButton) radioGroup.getChildAt(i)).setText(answers[i]);
}
}
所以基本上上面的解決方案只是你的問題的想法,希望它會幫助你。
嘗試使用getIdentifier(),可能是這樣的:
private void updateQuestion(int currentQuestion) {
// Update Questions
String questionString = "Q" + currentQuestion;
TextView textView = (TextView)findViewById(R.id.QuestionLabel);
textView.setText(getString(getResources().getIdentifier(questionString, "string", this.getPackageName())));
// Update Answers
String answerString;
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.rgQuestions);
for (int i = 0; i < radioGroup .getChildCount(); i++)
{
answerString = questionString + "A" + i;
((RadioButton) radioGroup.getChildAt(i)).setText(getString(getResources().
getIdentifier(answerString, "string", this.getPackageName())));
}
}
希望它有幫助:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.