0

タイトルの通り、4 つのタブが 4 つあるアプリを書いてい
ます タブ(SatrtActivity) で tabHost.setCurrentTab(3) を使用してタブ(3) に移動するにはどうすればよいですか??

例: tabHost を static として宣言する方法は?? またはいくつかの他の方法:)

これが私のコードです:

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)
     }

}
4

1 に答える 1

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

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

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

I think you're misunderstanding the use of static here. Static is used to make a member common across all instances of a class. It doesn't provide any intrinsic access benefits.

于 2012-11-16T06:18:53.343 に答える