Tabhost活动的一个活动与当前项目

时间:2014-02-11 11:20:23

标签: android android-tabhost android-tabs

我正在创建一个Android应用程序。 它有一个欢迎屏幕,其中有四个按钮(a,b,c,d)。

单击任意按钮(a,b,c,d)时,它将转到第二个活动。 下一个活动有(tabhost)四个选项卡(a,b,c,d)。

它如何运作 -

当我点击欢迎屏幕中的“a”按钮时,它会转到第二个活动的“标签”,另一个标签也在工作。

当我点击欢迎屏幕中的“b”按钮时,它会转到第二个活动的“b标签”,另一个标签也在工作。

当我点击欢迎屏幕中的“c”按钮时,它会转到第二个活动的“c tab”,另一个标签也在工作。

当我点击欢迎屏幕中的“d”按钮时,它会进入第二个活动的“d tab”,另一个标签也在工作。

    public class Dashboard extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dashboard);
    }

    public void helpB(View v) {
        Button clickedButton = (Button) v;
        switch (clickedButton.getId()) {
        case R.id.points:
            Intent i = new      Intent(getApplicationContext(),AndroidActivity.class);
            startActivity(i);
            break;

        case R.id.Search:
            Intent i1 = new Intent(getApplicationContext(),AppleActivity.class);
            startActivity(i1);
            break;

        case R.id.AboutUs:
            Intent i2 = new       Intent(getApplicationContext(),BlackBerryActivity.class);
    startActivity(i2);
            break;

        case R.id.ContactUs:
            Intent i3 = new Intent(getApplicationContext(),WindowsActivity.class);
            startActivity(i3);
            break;

        }
    }

}

和第二个活动代码 - >

     public class MainActivity extends TabActivity {

          public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Resources ressources = getResources();
        TabHost tabHost = getTabHost();

        // Android tab
        Intent intentAndroid = new Intent().setClass(this,
                AndroidActivity.class);
        TabSpec tabSpecAndroid = tabHost
                .newTabSpec("Android")
                .setIndicator("Points",
                        ressources.getDrawable(R.drawable.icon_android_config))
                .setContent(intentAndroid);

        // Apple tab
        Intent intentApple = new Intent().setClass(this, AppleActivity.class);
        TabSpec tabSpecApple = tabHost
                .newTabSpec("Apple")
                .setIndicator("Search",
                        ressources.getDrawable(R.drawable.icon_apple_config))
                .setContent(intentApple);

        // Windows tab
        Intent intentWindows = new Intent().setClass(this,
                WindowsActivity.class);
        TabSpec tabSpecWindows = tabHost
                .newTabSpec("Windows")
                .setIndicator("About us",
                        ressources.getDrawable(R.drawable.icon_windows_config))
                .setContent(intentWindows);

        // Blackberry tab
        Intent intentBerry = new Intent().setClass(this,
                BlackBerryActivity.class);
        TabSpec tabSpecBerry = tabHost
                .newTabSpec("Berry")
                .setIndicator(
                        "Contact us",
                        ressources
                                .getDrawable(R.drawable.icon_blackberry_config))
                .setContent(intentBerry);

        // add all tabs
        tabHost.addTab(tabSpecAndroid);
        tabHost.addTab(tabSpecApple);
        tabHost.addTab(tabSpecWindows);
        tabHost.addTab(tabSpecBerry);

        // set Windows tab as default (zero based)
        tabHost.setCurrentTab(0);
    }

}

1 个答案:

答案 0 :(得分:0)

你需要这个吗?

public void helpB(View v) {
Button clickedButton = (Button) v;
int tab = 0;
Intent i = new Intent(getApplicationContext(),MainActivity.class);
switch (clickedButton.getId()) {
case R.id.points:
    tab = 0;
    break;
case R.id.Search:
    tab = 1;
    break;

case R.id.AboutUs:
    tab = 2;
    break;

case R.id.ContactUs:
    tab = 3;
    break;

}
i.putExtra("extra.tab", tab);
startActivity(i);
}

和MainActivity:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Resources ressources = getResources();
TabHost tabHost = getTabHost();

// Android tab
Intent intentAndroid = new Intent().setClass(this,
        AndroidActivity.class);
TabSpec tabSpecAndroid = tabHost
        .newTabSpec("Android")
        .setIndicator("Points",
                ressources.getDrawable(R.drawable.icon_android_config))
        .setContent(intentAndroid);

// Apple tab
Intent intentApple = new Intent().setClass(this, AppleActivity.class);
TabSpec tabSpecApple = tabHost
        .newTabSpec("Apple")
        .setIndicator("Search",
                ressources.getDrawable(R.drawable.icon_apple_config))
        .setContent(intentApple);

// Windows tab
Intent intentWindows = new Intent().setClass(this,
        WindowsActivity.class);
TabSpec tabSpecWindows = tabHost
        .newTabSpec("Windows")
        .setIndicator("About us",
                ressources.getDrawable(R.drawable.icon_windows_config))
        .setContent(intentWindows);

// Blackberry tab
Intent intentBerry = new Intent().setClass(this,
        BlackBerryActivity.class);
TabSpec tabSpecBerry = tabHost
        .newTabSpec("Berry")
        .setIndicator(
                "Contact us",
                ressources
                        .getDrawable(R.drawable.icon_blackberry_config))
        .setContent(intentBerry);

// add all tabs
tabHost.addTab(tabSpecAndroid);
tabHost.addTab(tabSpecApple);
tabHost.addTab(tabSpecWindows);
tabHost.addTab(tabSpecBerry);

// set Windows tab as default (zero based)
int tab = getIntent().getExtras().getInt("extra.tab");
tabHost.setCurrentTab(tab);
}