2013-01-14 66 views
0

我有first.class和second.class。 如何通過點擊按鈕在first.class中更改ContentView(xml文件),誰在second.class中? (也許我的問題是簡單的,但我找不到答案。)如何更改其他課程的內容視圖?

回答

1

您可以使用一個簡單的標誌。從Bundle或磁盤中讀取第一個活動中的標誌(請參閱Data Storage)。在第一個活動的onCreate()方法使用這樣的事情:

// Read the flag, in this case from an Intent 
int choice = 0; 
Intent intent = getIntent(); 
if(intent != null) 
    choice = intent.getIntExtra(LAYOUT_PREFERENCE, 0); 

// Load the appropriate layout 
switch(choice) { 
case 0: 
    setContentView(R.layout.one); 
    break; 
case 1: 
    setContentView(R.layout.two); 
    break; 
//etc 
} 

設置該標誌的第二個活動,特別是裏面的按鈕的OnClickListener。再次,我用一個簡單的包,你可以通過setResult()傳遞,甚至startActivity()

0

你要保存設置你的第一個活動(無論是佈局1或佈局2)地方,其中兩個活動都可以訪問的價值和你的第二個活動可以寫它也是。您可以使用SharedPreferences,靜態變量或幾乎所有提到的in the Android Developer Guide

如果您始終從第二個活動開始第一個活動,您還可以將該佈局作爲額外添加到調用意圖中。

相關問題