(Android)如何更改子活动中的当前选项卡?

时间:2012-11-16 05:58:31

标签: android tabs android-tabhost

作为标题,我正在写一个有四个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)
     }

}

1 个答案:

答案 0 :(得分:0)

// get the parent activity
TabActivity parent = (TabActivity) getParent();

// get the tab host
TabHost tabhost    = parent.getTabHost();

// set the new tab
tabhost.setCurrentTab(3);

我认为你在这里误解了static的用法。静态用于使成员在所有类的实例中通用。它没有提供任何固有的访问权益。