英文:
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 = { "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
<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?
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论