cle*_*ent 0 static android tabactivity android-tabhost
我问我们是否可以在另一个类的tabhost上调用setCurrentTab而不是包含tabhost和tabspecs的类?
我们可以放
tabHost.setCurrentTab(1);
Run Code Online (Sandbox Code Playgroud)
在另一个班级而不是这个班级:
public class Main extends TabActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("", "Welcome in Main");
setContentView(R.layout.tab);
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost); // Le TabHost a des Tabs
TabSpec firstTabSpec = tabHost.newTabSpec("tid1"); // TabSpec: new tab - TabSpec : setContent to the tab
firstTabSpec.setIndicator("Informations", getResources().getDrawable(R.drawable.database)).setContent(new Intent(this,FirstTab.class));
tabHost.addTab(firstTabSpec);
TabSpec secondTabSpec = tabHost.newTabSpec("tid1");
secondTabSpec.setIndicator("Graphiques", getResources().getDrawable(R.drawable.chart)).setContent(new Intent(this,SecondTab.class));
tabHost.addTab(secondTabSpec);
TabSpec thirdTabSpec = tabHost.newTabSpec("tid1"); // tid1 is firstTabSpec Id (used to access outside)
thirdTabSpec.setIndicator("Réglages", getResources().getDrawable(R.drawable.settings)).setContent(new Intent(this,ThirdTab.class));
tabHost.addTab(thirdTabSpec);
}
}
Run Code Online (Sandbox Code Playgroud)
我们可以把它变成静态变量吗?我们怎样才能做到这一点?
感谢您的关注!
你确定可以.对于您的情况,TabActivity的单例可能效果最佳.例如:
public class Main extends TabActivity{
private staic Main theInstance;
public static getInstance() {
return Main.theInstance;
}
public Main() {
Main.theInstance = this;
}
// The rest of your code here.
}
Run Code Online (Sandbox Code Playgroud)
然后,从另一个班级,你可以打电话:
.Main.getInstance()getTabHost()setCurrentTab(1).
注意:我提供的并不是一个完整的单例实现,但它应该足以满足您的需求.根据您正在设置选项卡的类,您可能希望Main.getInstance()
在调用setCurrentTab()
方法之前检查它是否为null .
归档时间: |
|
查看次数: |
4473 次 |
最近记录: |