简体   繁体   English

如何将TextView的文本设置为另一个类的整数值

[英]How do i set the text of a TextView to a value of an integer from another class

How do i set the text of a TextView to a value of an integer from another class. 如何将TextView的文本设置为另一个类的整数值。

the main activity where i want the text set below 我要在下面设置文本的主要活动

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);


   TextView counterText = (TextView) findViewById(R.id.counter);


   counterText.setText();


}

the class below 下面的课程

public class Ship implements Serializable {

        private static int counter = 0;
        public int getCounter()
        {
            return counter;
        }

    }

For a starter this should help 对于初学者,这应该有所帮助

Ship s=new Ship();
int i=s.getCounter();
String value=String.valueOf(i);
counterText.setText(value);

使用String.valueOf在TextView中显示Integer,如下所示:

counterText.setText(String.valueOf(new Ship().getCounter()));

As far as i know you cannot access the UI component of 2nd activity while being in first activity. 据我所知,您在第一个活动中无法访问第二个活动的UI组件。 So the best way is to pass the value through putExtra method of intents and then set it to textview in the onCreate of 2nd activity by getting it. 因此,最好的方法是通过putExtra方法传递值,然后通过获取第二个活动的onCreate将其设置为textview Here is link which shows this method: http://mobileorchard.com/android-app-development-using-intents-to-pass-data-and-return-results-between-activities/ 这是显示此方法的链接: http : //mobileorchard.com/android-app-development-using-intents-to-pass-data-and-return-results-between-activities/

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

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