java - 如何以编程方式创建其中包含 EditText 的选项卡?

标签 java android android-tabhost

我的应用程序的文本编辑器允许用户打开和编辑文件,我想将文件作为 TabHost 中的新选项卡打开,以便可以打开多个文件。如何将 EditText 添加到新创建的选项卡?这是我在 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());

最佳答案

您尝试设置一个 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;
                 }
             });

关于java - 如何以编程方式创建其中包含 EditText 的选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17582749/

相关文章:

java - Spring Batch Reader 的多个 SELECT SQL - 读取器链接

java - 我可以为 Virgo OSGi bundle 创建单独的属性文件吗?

android - 如何在 Android 应用程序中实现评级功能?

android - 在 android 中完全自定义标签

android - 在 Android 设计库中使用 TabLayout 的带有图标的选项卡

java - 从 LTW 的属性文件中读取 Aspectj 切入点定义

c# - Android Bitmap.Compress 给 byte[] 提供 -1 字节?

android - 如何将数据设置为由 fragment 膨胀的布局

java-如何生成6位随机十六进制值

android - 在 android 的 QTextEdit 中选择