简体   繁体   English

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

[英]How to update the text of radio buttons with strings.xml

I build an application which asks the user several questions. 我构建了一个应用程序,询问用户几个问题。 Each question has 4 answers (radio buttons) which the user need to choose. 每个问题都有4个答案(单选按钮),用户需要选择。 I want to build one activity which updates with the questions and answers. 我想构建一个用问题和答案更新的活动。

All the questions and answers are written in strings.xml : 所有问题和答案都写在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>

.... ....

(I want to be able to add more questions in the future via this file and not in hard coded way) (我希望将来可以通过此文件添加更多问题,而不是以硬编码的方式)

When the user choose to get to the next question, I want to update the questions and answers via dynamic way: 当用户选择进入下一个问题时,我想通过动态方式更新问题和答案:

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));
        }
    } 

But how can I set the text of the radio buttons (and textView) via strings.xml by choosing the right string value ? 但是如何通过选择正确的字符串值来设置通过strings.xml的单选按钮(和textView)的文本? (I want to build the StringRes with currentQuestion and i iterator). (我想用currentQuestion和i迭代器构建StringRes)。 Is it possible? 可能吗?

You can use String Array : 您可以使用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>

And access it via: 并通过以下方式访问:

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);

In your example: 在你的例子中:

    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]);
            }

        } 

So basically above solution is just idea to your question, hope it will help you. 所以基本上上面的解决方案只是你的问题的想法,希望它会帮助你。

Try to use getIdentifier(), may be something like this: 尝试使用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())));
    }
}

Hope it help :) 希望它有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM