作为标题,我正在编写一个有四个 4 个选项卡的应用程序
如何在 tab(SatrtActivity) 中使用 tabHost.setCurrentTab(3) 转到 tab(3)?
例如:如何将 tabHost 声明为静态?或其他方式:)
这是我的代码:
public class MainActivity extends TabActivity {
public static TabHost tabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, StartActivity.class);
spec = tabHost.newTabSpec("tab1").setIndicator("Start",res.getDrawable(R.drawable.start))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, WeatherActivity.class);
spec = tabHost.newTabSpec("tab2").setIndicator("Weather",res.getDrawable(R.drawable.weather))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, TrafficActivity.class);
spec = tabHost.newTabSpec("tab3").setIndicator("Traffic",res.getDrawable(R.drawable.traffic))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, NewsActivity.class);
spec = tabHost.newTabSpec("tab4").setIndicator("News",res.getDrawable(R.drawable.price))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
public class StartActivity extends Activity{
TextView tempNum, cn1, cn2, cn3, ln1, ln2, ln3;
final Handler myHandler = new Handler();
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView(R.layout.start);
tempNum = (TextView) findViewById(R.id.temp);
cn1 = (TextView) findViewById(R.id.zone1News1);
cn2 = (TextView) findViewById(R.id.zone1News2);
cn3 = (TextView) findViewById(R.id.zone1News3);
ln1 = (TextView) findViewById(R.id.zone2News1);
ln2 = (TextView) findViewById(R.id.zone2News2);
ln3 = (TextView) findViewById(R.id.zone2News3);
Timer myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {updateInfo();}
}, 20000, 5000);
}
private void updateInfo() {
myHandler.post(myRunnable);
}
final Runnable myRunnable = new Runnable() {
public void run() {
try{
URL url = new URL("http://xml.weather.yahoo.com/forecastrss?p=TWXX0025&u=c");
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
WeatherHandler weatherHandler = new WeatherHandler();
CentralNewsOneHandler centralNewsOneHandler = new CentralNewsOneHandler();
CentralNewsTwoHandler centralNewsTwoHandler = new CentralNewsTwoHandler();
CentralNewsThreeHandler centralNewsThreeHandler = new CentralNewsThreeHandler();
xr.setContentHandler(weatherHandler);
xr.parse(new InputSource(url.openStream()));
WeatherParsedDataSet weatherParsedDataSet = weatherHandler.getParsedData();
tempNum.setText(weatherParsedDataSet.toString());
}catch (Exception e) {
e.printStackTrace();
}
}
};
public void setToNewsTab(View view)
{
tabHost.setCurrentTab(3);
//Here is the method that I wanna set to tab(3)
}
}