so what i am tring to do is, i am having a book like-app, and want to setup a button when click it change the textview to a string in the @string rec. but in a different way
i will put the code just a moment, lets say i have a string in @string that is NH1,NH2,NH3 i can do for button View.setText(R.string.NH1); that will work but i want to create a counter and make it so every time clicked counter++ and want it to be like this View.setText(R.string.NH+counter); if you know what i mean so each time it clicked to goes to next @string without changing layout every time and without an array because the paragraph are very long
You could do this by creating an XML string array, so your strings will still be referenced in resources, but you'll be able to loop through them.
您可以通过创建XML字符串数组来完成此操作,因此您的字符串仍将在资源中引用,但您将能够遍历它们。
Something like this, in values/strings.xml:
在values / strings.xml中这样的东西:
MercuryVenusEarthMars
And then in Activity:
然后在活动中:
Resources res = getResources();
final String[] planets = res.getStringArray(R.array.planets_array);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (counter
Be sure to check the counter against arrays length, otherwise you will get an IndexOutOfBoundsException.