How do i set the text of a TextView to a value of an integer from another class.
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. 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. Here is link which shows this method: http://mobileorchard.com/android-app-development-using-intents-to-pass-data-and-return-results-between-activities/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.