我的应用程序的文本编辑器允许用户打开和编辑文件,我想将文件作为 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/