简体   繁体   English

将TextView设置为整数值

[英]Setting a TextView to the value of an integer

public class DataHelper extends Activity{

    TextView tvRslt;

    public static int insert(String firstFB, String firstRL) {
        // TODO Auto-generated method stub
        Integer a = new Integer(firstFB).intValue();
        Integer b = new Integer(firstRL).intValue();
        int firstResult = a / b;
        }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);

I wanted to set the integer "firstResult" to display on the TextEdit "tvRslt". 我想设置整数“ firstResult”以在TextEdit“ tvRslt”上显示。

How do I do this? 我该怎么做呢?

If any further information is required, let me know. 如果需要任何其他信息,请告诉我。

With exception handling: 使用异常处理:

try {
    tvRslt.setText(firstResult + "");
} catch (DivideByZeroException e) {
    // Do something with exception condition.
}

Without exception handling: 无异常处理:

// Before dividing check if denominator is 0.
int firstResult = (b == 0) ? 0 : a / b;
tvRslt.setText(firstResult + "");

This does of course require that you have a reference to the textview, as I assume you have in the code you did not show us. 当然,这确实需要您对textview进行引用,因为我假设您在代码中没有显示给我们。

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

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