[英]How do I programmatically create a Tab with EditText in it?
我的应用程序的文本编辑器允许用户打开和编辑文件,我想在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());
您尝试将一个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;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.