2012-02-09 43 views
0

如何獲取對TextView的引用,例如,如果我沒有將佈局設置爲內容視圖?訪問另一個佈局中的視圖

我使用TabHost的活動之間進行切換:

public class TimerResultsActivity extends TabActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     initTabs(); 

     TextView resultsText = (TextView)findViewById(R.id.textView1); 
     resultsText.setText("test"); 
    } 

...

「textView1」 已經加入到標籤中的一個,但在main.xml中沒有宣佈。 如何更改上面的文字?

回答

2

您需要參考其中聲明的的佈局的基準。然後您可以使用它所屬的ViewGroupfindViewById()方法。

TextView resultsText = (TextView)myTab.findViewById(R.id.textView1); 
... 
+0

也必須充氣佈局。 – Shaiful 2012-02-09 16:27:35

相關問題