Android (change string in java code)

In the / res / values ​​folder of my android project, I have a line referenced by a text view in my XML file, I want to change the line in my java file.

As you can see below in the code, I made a string variable, and then below what I set, for which the string variable is set, where is the string. where I have the "here" in the code, where I want to change the line in the values ​​folder. but I don’t know what code to use to install it.

I could just change the text in the text view from my java file, which I know how to do, but this is the old way and warning sets, so I would prefer to use the line that is the best way to do this.

With my knowledge of changing text in a textual representation, I basically guessed about my path to this stage, but I don’t know how to go further, can someone give me some advice on what to do, thanks.

String string; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); counter = 0; add = (Button) findViewById(R.id.badd); sub = (Button) findViewById(R.id.bsub); reset = (Button) findViewById(R.id.breset); display = (TextView) findViewById(R.id.tvdisplay); string = (String) getString(R.string.counter); add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub ((///////////////here////////////////)) counter++; } }); 
+6
source share
3 answers

You told us a lot of text changes, but you did not say what should be in the text. I also need to guess:

The strings.xml file should be used for texts that may change for different languages. If you just want to change the counter text, you should not do this via strings.xml, since the numbers are universal :)

Try to go with this:

 display.setText(String.valueOf(counter)); 
+2
source

You cannot change the text assigned to the <string> elements of the /res/values/strings.xml file at run time. They are constants so effectively final .

You also cannot change the XML layout file at run time. If you created a layout with a TextView that has the android:text attribute set to some initial line of the resource, this is basically an “initial” value and cannot be changed to anything else at run time.

+3
source

You want to use the setText () method.

 display.setText("text"); 
0
source

Source: https://habr.com/ru/post/904864/


All Articles