2012-12-21 53 views
0

在我的活動中,我有一個TabHostAndroid TabActivity:如何僅從當前選項卡開始活動?

我有3個選項卡和3個活動。如何在點擊選項卡時開始相應的活動?

在所有三個活動開始的那一刻...

如果我運行此代碼,每一個活動(connected_uploadconnected_downloadconnected_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); 
      }  
     }); 

    } 
} 

回答

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

的main.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); 
    } 

} 

thirdtab.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); 
     } 

    } 
+0

我會盡快嘗試。感謝和快樂的X-MAS :) –

+0

這個作品:D非常感謝你; D –

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); 
    } 
} 
+0

我在我的主要活動中有一個onCreate方法。看看我的編輯代碼的問題。 –

+0

抱歉,但我不明白。 –

+0

我已經有了代碼,您發給我了...... –

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

    } 
} 
+0

這不起作用......所有這三項活動都開始......默認情況下,所有三項活動都會開始... –

+0

首先打開您選擇的選項卡。當你點擊剩下的兩個標籤時,相應的活動就會到來。代碼正在工作。笏你的要求? – User

+0

不,當我啓動我的應用程序時,所有三個活動的onCreate方法都將啓動。 –

相關問題