我是android的新手,在我的android應用中,主要的Activity
類,其中包含TextView
用於顯示來自其他類的各種狀態消息。我想更新主Activity
的TextView
與其他類的狀態值。主要活動類和其他類沒有直接聯繫。在android中可能嗎?如果是的話,我不知道要這樣做。請提供解決方案做如何在主活動中的TextView上顯示狀態消息?
代碼片段
//main activity
public class MainMenu extends Activity {
static String status = "Hello Friends";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView)findViewById(R.id.mytext);
tv.setText(status);
MyOtherClass myclass = new MyOtherClass();
myclass.connect();
}
其他類不是一個活動類
// Other class
public class MyOtherClass {
public MyOtherClass(){
}
public void connect(){
String strIP = Mtx.confRead("IPAddress");
String strPort = Mtx.confRead("Port");
String msg = "Connecting...";
// i want to show value of msg varible in Textview of main activity from here
}
感謝您
需要刷新的TextView以反映更改後的值。 –
如何刷新? – Riskhan
有很多方法可以做到這一點。你可以使用TimerTask。你也可以嘗試Handler類來刷新UI。 –