如何以编程方式创建包含EditText的Tab?

时间:2013-07-10 23:38:34

标签: java android android-tabhost

我的应用程序的文本编辑器允许用户打开和编辑文件,我想将文件作为TabHost中的新选项卡打开,因此可以打开多个文件。如何将EditText添加到新创建的Tab?这是我在onCreate()

中尝试过的
TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
        tabHost.setup();
        EditText editor = new EditText(this);
        TabSpec spec1=tabHost.newTabSpec("Tab 1");
        spec1.setContent(editor.getId());
        spec1.setIndicator("Tab 1");

我认为问题是`spec1.setContent(editor.getId());

1 个答案:

答案 0 :(得分:1)

您尝试将ID(未通过此方式定义)设置为布局ID。 它不会那样工作。尝试:

TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
        tabHost.setup();
        EditText editor = new EditText(this);
        TabSpec spec1=tabHost.newTabSpec("Tab 1");
        spec1.setIndicator(editor); 

如果这是你想要的。您也可以尝试:

TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
            tabHost.setup();
            final EditText editor = new EditText(this);
            TabSpec spec1=tabHost.newTabSpec("Tab 1");
            spec1.setContent(new TabHost.TabContentFactory(){
                 public View createTabContent(String tag){
                     return editor;
                 }
             });