2012-04-20 43 views
1

我有3個佈局; main.xml中,secondscreen.xml,thidrscreen.xmlif(當前屏幕== R.layout.main)

我要上後退按鈕覆蓋像 currentscreen(其屏幕打開)

if(currentscreen == R.layout.main) 
{ 
    do something 
} 
    else (currentscreen == R.layout.secondscreen) 
    { 
    do something 
    } 

我怎麼能這樣做?

任何幫助將不勝感激?

+1

爲什麼不爲每個佈局3個不同的活動? ,你是否想要做? – DGomez 2012-04-20 17:54:45

+0

+1以適應不同的活動。當你嘗試在一個活動中做所有事情時,它變成一個痛苦管理變量和所有代碼。 – dymmeh 2012-04-20 17:55:21

+0

我害怕在活動之間使用Intent。因爲我有太多的東西在我的方法之間共享(這是佈局).. – Merve 2012-04-20 17:57:27

回答

3

我不知道爲什麼你要做到這一點,就是這樣,但如果你有你的佈局中的最高級別視圖的ID,你也許可以這樣做:

if (getContentView().getId() == R.id.myTopLevelViewFromMain){ 
     // do stuff 
} else if (getContentView().getId() == R.id.myTopLevelViewFromSecondScreen){ 
     // do other stuff 
} 

或者,如您對問題的評論(這可能是更好的設計選擇)建議,您可以使用多個活動。

第三種方法是在佈局中使用3個頂層視圖,並根據需要操縱其可見性。