如何使用strings.xml更新单选按钮的文本

时间:2017-01-16 03:27:50

标签: android

我构建了一个应用程序,询问用户几个问题。 每个问题都有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)。 有可能吗?

2 个答案:

答案 0 :(得分:0)

您可以使用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]);
            }

        } 

所以基本上上面的解决方案只是对你的问题的想法,希望它能帮到你。

答案 1 :(得分:0)

尝试使用getIdentifier(),可能是这样的:

Sub TransposeColumns()
Dim i As Long
Dim RW As Long

With Sheets("ORIGINAL")
For i = 4 To 10 'CHANGE VALUE HERE; MAX NUMBER SHOULD BE NUMBER OF  COLUMNS IN ORIGINAL WORKSHEET
     RW = 1 + Sheets("output").Range("A65536").End(xlUp).Row
     .Range("A2:C74").Copy Sheets("output").Range("A" & RW) 'CHANGE RANGE VALUES
     .Range(.Cells(2, i), .Cells(74, i)).Copy Sheets("output").Range("D" & RW) 'CHANGE RANGE VALUES
     Sheets("output").Range("E" & RW).Resize(73) = .Cells(1, i).Value
     'CHANGE RESIZE VALUE;RESIZE VALUE SHOULD BE MAX RANGE VALUE - 1
 Next i
 End With
 End Sub

希望有所帮助:)