1

我正在尝试显示我设置的文本TextView。我已经分配了一个strings.xml名为operator_mixed的字符串

字符串.xml

<string name="operator_mixed">%d  %s %d  %s %d</string>

在我的 initialize.java 文件中,我尝试引用此字符串,但它不显示任何内容。但是,当我尝试显示没有任何格式的文本时,它有点工作,但我知道在 textview 中连接字符串并不可取。

初始化.java

textview.setText(getString(R.string.operator_mixed, a1, operator1, a2, operator2, a3));

其中 a1, a2, a3 是整数,operator1, operator2 是字符串。

我的代码有问题吗?

4

3 回答 3

0

试试下面

<string name="operator_mixed">%d  %s %d  %s %d</string>
textview.setText(String.format(getString(R.string.operator_mixed),a1,operator1,a2,operator2,a3)));

你必须使用String.format()

于 2019-04-19T07:44:00.257 回答
0

我刚刚在我的应用程序中尝试了这段代码。

string.xml:(和你的一样)

<string name="operator_mixed">%d  %s %d  %s %d</string>

我的活动.java

int a1 = 10;
int a2 = 20;
int a3 = 30;
String operator1 = "Operator 1";
String operator2 = "Operator 2";

my_txt_title.setText(getResources().getString(R.string.operator_mixed, a1, operator1, a2, operator2, a3));

它工作正常。

在此处输入图像描述

于 2019-04-19T07:59:24.897 回答
0

我想到了。我只需要像这样传递一个上下文:

textview.setText(c.getResources().getString(R.string.operator_mixed, a1, operator1, a2, operator2, a3))

这里 c 是 MainActivity 的上下文。

于 2019-04-19T08:17:59.927 回答