0

在我的活动中,我有一个TabHost.

我有 3 个标签和 3 个活动供他们使用。单击选项卡时如何启动相应的活动?

目前这三个活动都开始了……

如果我运行此代码,每个活动 ( connected_upload, connected_download, connected_search) 都会运行 " onCreate" 方法。

如何手动启动这些活动?我的意思是我只想在单击相应的选项卡时才开始活动......

public class connected extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.connected);

        Resources res = getResources(); // Resource object to get Drawables   
        TabHost tabHost = getTabHost();  // The activity TabHost  
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab    
        Intent intent;  // Reusable Intent for each tab   

        intent = new Intent().setClass(this, connected_upload.class);      
        spec = tabHost.newTabSpec("Hoch").setIndicator("Hoch",res.getDrawable(R.drawable.freeftp)).setContent(intent); 
        tabHost.addTab(spec);   

        intent = new Intent().setClass(this, connected_download.class); 
        String str_path = getIntent().getStringExtra("path");
        String str_profil = getIntent().getStringExtra("profil");
        String str_server = getIntent().getStringExtra("server");
        String str_port = getIntent().getStringExtra("port");
        String str_user = getIntent().getStringExtra("user");
        String str_password = getIntent().getStringExtra("pw");

        intent.putExtra("path", str_path);
        intent.putExtra("profil", str_profil);
        intent.putExtra("server", str_server);
        intent.putExtra("port", str_port);
        intent.putExtra("user", str_user);
        intent.putExtra("pw", str_password);

        spec = tabHost.newTabSpec("Herunter").setIndicator("Herunter",res.getDrawable(R.drawable.freeftp)).setContent(intent); 
        tabHost.addTab(spec); 

        intent = new Intent().setClass(this, connected_search.class); 
        spec = tabHost.newTabSpec("Search").setIndicator("Search",res.getDrawable(R.drawable.freeftp)).setContent(intent); 
        tabHost.addTab(spec);

        tabHost.setCurrentTab(1);

        //Button Connect Server
        Button cmd_mainsite = (Button)findViewById(R.id.but_connected_mainsite);
        cmd_mainsite.setOnClickListener(new View.OnClickListener(){         
            public void onClick(View v){
                finish();
                Intent Intent_mainsite = new Intent(connected.this, Login.class);
                startActivity(Intent_mainsite); 
            }       
        });

    }
}
4

3 回答 3

2

根据您的要求使用此代码

MainActivity.java

public class MainActivity extends TabActivity {


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

                           Resources res = getResources();
                            TabHost th = getTabHost();

                            th.addTab(th.newTabSpec("").setIndicator("tab1",
                                    res.getDrawable(R.drawable.icon)).
                                    setContent(new Intent(this, firsttab.class)));
                            th.addTab(th.newTabSpec("").setIndicator("tab2")
                                    .setContent(new Intent(this, secondtab.class)));
                            th.addTab(th.newTabSpec("").setIndicator("tab3")
                                    .setContent(new Intent(this, thirdtab.class)));

    th.setCurrentTab(1);
                }
                }

主要的.xml

<?xml version="1.0" encoding="utf-8"?>

    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/tabhost" 
        android:layout_width="match_parent"
        android:layout_height="match_parent">
            <LinearLayout 
            android:layout_width="match_parent" 
            android:id="@+id/linearLayout1" 
            android:layout_height="match_parent" 
            android:orientation="vertical">

                <FrameLayout 
                android:layout_width="match_parent"
                 android:layout_height="match_parent" 
                 android:id="@android:id/tabcontent"
                 android:layout_weight="1">

                </FrameLayout>
                 <TabWidget 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:id="@android:id/tabs">
                </TabWidget>
            </LinearLayout>
        </TabHost>

firsttab.java

public class firsttab extends Activity {

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // TODO Auto-generated method stub
            TextView tv = new TextView(this);
            tv.setText("HI");
            tv.setTextSize(25);
            setContentView(tv);
        }

    }

secondtab.java

public class secondtab extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // TODO Auto-generated method stub
        TextView tv = new TextView(this);
        tv.setText("Hello");
        tv.setTextSize(25);
        setContentView(tv);
    }

}

第三个tab.java

public class thirdtab extends Activity {

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // TODO Auto-generated method stub
            TextView tv = new TextView(this);
            tv.setText("How Are U?");
            tv.setTextSize(25);
            setContentView(tv);
        }

    }
于 2012-12-24T08:28:13.863 回答
0

为单击特定选项卡时的打开活动编写下面的代码,如果您对此有任何疑问,请告诉我。

TabActivity.java:-

public class TabActivity extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_screen);
        TabHost tabHost = getTabHost();

        Intent intent = new Intent().setClass(this, ActivityStack.class);
        TabHost.TabSpec spec = tabHost.newTabSpec("tabId").setIndicator("Temp", getResources().getDrawable(R.drawable.home));
        spec.setContent(intent);

        tabHost.addTab(spec);

        Intent intent1 = new Intent().setClass(this, ActivityStack.class);
        TabHost.TabSpec spec1 = tabHost.newTabSpec("tabId").setIndicator("Temp", getResources().getDrawable(R.drawable.invoice));
        spec1.setContent(intent1);

        tabHost.addTab(spec1);

        tabHost.setCurrentTab(0);
    }
}
于 2012-12-21T07:30:23.867 回答
0
public class connected extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.connected);

        Resources res = getResources(); // Resource object to get Drawables   
        TabHost tabHost = getTabHost();  // The activity TabHost  
TabSpec spec1= tabHost.newTabSpec("")
                .setIndicator("Hoch",res.getDrawable(R.drawable.freeftp))
                .setContent(new Intent(this, connected_upload.class));
        tabHost.addTab(spec1);

        TabSpec spec2 = tabHost.newTabSpec("")
                .setIndicator("Herunter",res.getDrawable(R.drawable.freeftp))
                .setContent(new Intent(this, connected_download.class));



        String str_path = getIntent().getStringExtra("path");
        String str_profil = getIntent().getStringExtra("profil");
        String str_server = getIntent().getStringExtra("server");
        String str_port = getIntent().getStringExtra("port");
        String str_user = getIntent().getStringExtra("user");
        String str_password = getIntent().getStringExtra("pw");

        intent.putExtra("path", str_path);
        intent.putExtra("profil", str_profil);
        intent.putExtra("server", str_server);
        intent.putExtra("port", str_port);
        intent.putExtra("user", str_user);
        intent.putExtra("pw", str_password);

        tabHost.addTab(spec2);

    TabSpec spec3= tabHost.newTabSpec("")
                .setIndicator("Search",res.getDrawable(R.drawable.freeftp))
                .setContent(new Intent(this, connected_search.class));
        tabHost.addTab(spec3);

      tabHost.setCurrentTab(1);

        //Button Connect Server
        Button cmd_mainsite = (Button)findViewById(R.id.but_connected_mainsite);
        cmd_mainsite.setOnClickListener(new View.OnClickListener(){         
            public void onClick(View v){
                finish();
                Intent Intent_mainsite = new Intent(connected.this, Login.class);
                startActivity(Intent_mainsite); 
            }       
        });

    }
}
于 2012-12-21T08:17:50.523 回答