简体   繁体   中英

Concatenation in XML

Is it possible to concatenate a string from my java code for one of the activities to a string in another xml file? More specifically I have just added a popup Window and I would like it to give a message 'Congrats! You have obtained A', where A is the string from my activity and should be able to get different values. So basically my question is if there is a way to concatenate

 `public String letter;`

from my java file to

`android:text="@string/popup_text"`

from my xml file?

您必须参考您的TextView,然后才能执行以下操作:

textView.setText(textView.getText().toString() + letter);

Have a look at the API for string resources .

In your case, consider either:

textView.setText(getResources().getString(R.string.popup_text) + " " + letter);

Or to format the string resource with String.format by having the string resource look like

<string name="popup_text"> Congrats! You have obtained %1$s!</string>

And then in your activity or fragment:

Resources res = getResources();
testView.setText(String.format(res.getString(R.string.popup_text), letter);

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