如何通过tabHost中的id访问视图?

时间:2013-05-29 12:55:13

标签: java android android-tabhost

我有带动态创建标签的tabHost小部件。 在每个新创建的选项卡中,都有相对布局和一些视图(textView,listView等)。

我的问题是:如何访问这些视图以及如何在其中添加新内容?我尝试设置ID或标签,但我没有工作。

   public class Main extends Activity {

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

       final TabHost tabs=(TabHost)findViewById(R.id.tabhost);
       tabs.setup();   
       TabHost.TabSpec spec=tabs.newTabSpec("buttontab");
       spec.setContent(R.id.form);
       spec.setIndicator("Search-form");
       tabs.addTab(spec);

       Button btn=(Button)tabs.getCurrentView().findViewById(R.id.buttontab);

       btn.setOnClickListener(new View.OnClickListener() {



           public void onClick(View view) {
                final TabHost.TabSpec spec=tabs.newTabSpec("tag1");        
                spec.setContent(new TabHost.TabContentFactory() {
                public View createTabContent(String tag) 
                {
                   // Creating a new RelativeLayout
               RelativeLayout relativeLayout = new RelativeLayout(Main.this);
                   // Defining the RelativeLayout layout parameters.
                   RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                           RelativeLayout.LayoutParams.FILL_PARENT,
                           RelativeLayout.LayoutParams.FILL_PARENT);


                           TextView t = new TextView(Main.this);
                           // t.setId(12); //This dosen't work
                           t.setText("Searching in progress. Please wait...");

                           ListView resultsList = new ListView(Main.this);
                           //Here: array adapter etc

                          // Adding the TextView to the RelativeLayout as a child
                          relativeLayout.addView(t);
                          relativeLayout.addView(resultsList);

                          return relativeLayout;

                       }
                   });
                   spec.setIndicator("Tab with results");
                   tabs.addTab(spec);
                   new GetData(Main.this).execute();      
                }
            });
     }



     public void appendResultsToViews(String result)
     {
           //Here i need to access TextView from tab with specific tag and append results to TextView, which was created dynamically earlier
     }

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

设置子视图的标记并使用findViewWithTag(tag)