简体   繁体   中英

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

<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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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