可以使用按钮更改TextView的引用吗?

huangapple go评论113阅读模式
英文:

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

问题

我有这段代码:

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

但我还有一个 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>

我可以改变引用到 strings.xml 文件而不是改变文本值吗?

英文:

I have this code

final String[] instructionsStrings  = { &quot;instruction 1&quot;,
                                                &quot;instruction 2&quot;,
                                                &quot;instruction 3&quot;,
                                                &quot;instruction 4&quot;};
final int[] instructionIndex = {0};
ImageButton imageButtonNext = findViewById(R.id.imageButtonNext);
imageButtonNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
          if (instructionIndex[0] &lt; 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

&lt;string name=&quot;instructions_1&quot;&gt;instruction 1&lt;/string&gt;
&lt;string name=&quot;instructions_2&quot;&gt;instruction 2&lt;/string&gt;
&lt;string name=&quot;instructions_3&quot;&gt;instruction 3&lt;/string&gt;
&lt;string name=&quot;instructions_4&quot;&gt;instruction 4&lt;/string&gt;

can I instead of changing the text value change the reference to the strings.xml file?

答案1

得分: 1

你应该使用String资源来初始化instructionsStrings()

你的最终代码:

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

你可以在文档中阅读更详细的信息。

英文:

You should initialize instructionsStrings() with String resources.

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

huangapple
  • 本文由 发表于 2020年5月4日 06:29:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/61582484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定