设置文本为整数值 [英] Set Text to Integer Value

查看:103
本文介绍了设置文本为整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想成立一​​个TextView作为一个整数的价值,希望像这样:tv.setText(INT)

I want to set a TextView as the value of an integer, hopefully like so: tv.setText(int)

不过,整数值是另一个类中。

However, the integer value is inside another class.

我可以得到任何帮助吗?我是小白。

Can I get any help? I'm a noob.

推荐答案

在另一个类?您可以为您在班要值一个getter:

Inside another class? You can make a getter for the value you want in that class:

public int getValue() {
    return value;
}

所以,你可以从另一个访问:

So that you can access it from the other one:

tv.setText(theOtherClassInstance.getValue())

BUT 如果您设置一个TextView为int,这将是间preTED为Android的资源ID 的。如果你想为int值作为文本(而不是它所指向的资源),使它成为一个字符串第一。

BUT if you set a TextView to an int, it will be interpreted as an Android resource id. If you want the value of the int as your text (and not the resource it points to), make it a String first.

tv.setText(String.valueOf(theOtherClassInstance.getValue()));

修改如果你的int是 FIRSTRESULT 按下面评论,然后吸气变为:

EDIT If your int is firstResult as per your comment below, then the getter becomes:

public int getFirstResult() {
    return firstResult;
}

这篇关于设置文本为整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆