简体   繁体   English

如何在同一个 TextView 中设置字符串和双精度值?

[英]How can I set a string and a double in the same TextView?

I've been trying to put a double and a string in a textView on android studio, but everytime I try it ends up adding the string to the double like an adition, i tried putting comas instead of "+" but it marked error, maybe the answer it's pretty obvious but I haven found it till the momment this is what I have我一直在尝试在 android studio 上的 textView 中放置一个双精度和一个字符串,但是每次我尝试它最终都会像添加一样将字符串添加到双精度中,我尝试使用逗号而不是“+”,但它标记了错误,也许答案很明显,但直到那一刻我才找到它,这就是我所拥有的

在此处输入图片说明

Concatenate before you set the text:在设置文本之前连接:

String text = altura2 + AlturaPLUS;
textView.setText(text);

The + operator adds two operands if both of them are numbers.如果两个操作数都是数字,则 + 运算符将它们相加。 It performs the concatenation only if at least one of them is String .仅当其中至少一个是String时才执行连接。

For example.例如。

double a = 1.5;
double b = 2.5;

Now to to concatenate the above two numbers, you should use:现在要连接上述两个数字,您应该使用:

String result = ""+a+b;
//or String result = String.valueOf(a) + b;
textView.setText(result);

If you use textView.setText(""+a+b) you will get a warning related to localization.如果您使用textView.setText(""+a+b)您将收到与本地化相关的警告。 But your code will run fine.但是您的代码将运行良好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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