android 不能在基元类型 long 上调用子字符串(int, int)



我有文本视图,它会显示很大的数字,如果位数大于 5,我想让文本视图只显示 4 位带有这样的点(3544...)我试过了,但出现此错误:

android 不能在基元类型 long 上调用子字符串(int, int)

这是我的代码:

EditText EditNumber;
long  theNumber;
String str = EditNumber.getText().toString();
theNumber = Long.parseLong(str );
if( theNumber >5)
{
theNumber =  theNumber.substring(0,4)+"..."; // the error in this line.
textView1.setText(Long.toString(theSide));
}
else
{
 textView1.setText(Long.toString(theNumber));
 }

正如Martin Cazares所指出的,long没有子字符串。使用字符串而不是双精度值。

EditText EditNumber;
long  theNumber;
String str = EditNumber.getText().toString();
if( str.length() > 4) // > 4 digits
{
    textView1.setText(str.substring(0,4)+"...");
}
else
{
     textView1.setText(str);
 }

希望对您有所帮助!

Long.toString(theNumber).substring(x,y);

这应该给你你想要的数字。

您正在尝试对没有该方法的Long进行substring方法调用。 您可能打算改为str.substring(0, 0)

错误是"long"没有子字符串方法,小心使用原语,它们根本没有任何方法......

如果你想子字符串,可以这样做:

String theNumber = str.substring(0,4)+"...";
textView1.setText(theNumber);

但除此之外,你甚至可能不需要自己做,看看

android:ellipsis="end" 属性的 TextView

如果文本视图的大小小于实际文本,它将为您执行省略号。

问候!

最新更新