简体   繁体   English

Android - 我可以用按钮更改 TextView 的参考吗?

[英]Android - Can I change the reference of a TextView with a button?

I have this code我有这个代码

final String[] instructionsStrings  = { "instruction 1",
                                                "instruction 2",
                                                "instruction 3",
                                                "instruction 4"};
final int[] instructionIndex = {0};
ImageButton imageButtonNext = findViewById(R.id.imageButtonNext);
imageButtonNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
          if (instructionIndex[0] < 3){
              instructionIndex[0]++;
              instructions.setText(instructionsStrings[instructionIndex[0]]);
          }
      }
});

which changes a text view within an array, but i also have a strings.xml file which I have this strings它更改了数组中的文本视图,但我也有一个 strings.xml 文件,我有这个字符串

<string name="instructions_1">instruction 1</string>
<string name="instructions_2">instruction 2</string>
<string name="instructions_3">instruction 3</string>
<string name="instructions_4">instruction 4</string>

can I instead of changing the text value change the reference to the strings.xml file?我可以更改对 strings.xml 文件的引用而不是更改文本值吗?

You should initialize instructionsStrings() with String resources .您应该使用String resources初始化instructionsStrings()

Your final code:您的最终代码:

final String[] instructionsStrings  = { getString(R.string.instruction_1),
                                           getString(R.string.instruction_2),
                                           getString(R.string.instruction_3),
                                           getString(R.string.instruction_4)};

You can read more detailed information in the documentation您可以在文档中阅读更多详细信息

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

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