如何在TextView中显示Integer值?
当我尝试时,我收到一个错误 android.content.res.Resources$NotFoundException: String resource ID
Muh*_*lah 77
TextView tv = new TextView(this);
tv.setText(String.valueOf(number));
Run Code Online (Sandbox Code Playgroud)
要么
tv.setText(""+number);
Run Code Online (Sandbox Code Playgroud)
Fal*_*rri 39
TextView tv = new TextView(this);
tv.setText("" + 4);
Joa*_*Ley 13
如果您希望它显示在您的布局上,您应该
例如:
在activity.XML文件上
<TextView
android:id="@+id/example_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)
在activity.java文件中
final TextView textView = findViewById(R.id.example_tv);
textView.setText(Integer.toString(yourNumberHere));
Run Code Online (Sandbox Code Playgroud)
在XML文件的第一行中,您可以看到: findViewById(R.id.example_tv)
这就是你在.java文件中获取id的地方findViewbyId()
希望我自己说清楚,我只是接受了这个解释,因为很多时候人们似乎都知道".setText()"方法,他们只是无法改变"UI"中的文字.
小智 10
请考虑使用具有适当格式规范(%d或%f)的String#格式.
int value = 10;
textView.setText(String.format("%d",value));
Run Code Online (Sandbox Code Playgroud)
这将正确处理小数分隔符和特定于区域设置的数字
替代方法:
TextView tv = new TextView(this);
tv.setText(Integer.toString(integer));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
102924 次 |
最近记录: |