2011-07-13 97 views
2

我已經作出了選項卡主機和示出在其中4個標籤,如下面的代碼是TabHost中的另一項活動?

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

    setContentView(R.layout.main); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

    TabHost tabHost = getTabHost(); 

    tabHost.addTab(tabHost.newTabSpec("list").setIndicator("List").setContent(
      new Intent(this, List.class))); 
    tabHost.addTab(tabHost.newTabSpec("profile").setIndicator("Profile").setContent(
      new Intent(this, Profile.class))); 
    tabHost.addTab(tabHost.newTabSpec("criteria").setIndicator("Criteria").setContent(
      new Intent(this, Criteria.class))); 
    tabHost.addTab(tabHost.newTabSpec("more").setIndicator("More").setContent(
      new Intent(this, More.class))); 
    } 
} 

在資料,我有一個按鈕,用於將編輯的用戶簡檔,因爲我點擊該按鈕時,它調用方法如下

@Override 
public void onClick(View v) { 
System.out.println("Veer"); 
    Intent intent = new Intent().setClass(this, EditProfile.class); 
Intent intent = new Intent(this, EditProfile.class); 
} 

哪裏是顯示EditProfile,但標籤是不可見,因爲在iPhone它是可見的所有的時間,現在我應該怎麼做,這樣我的標籤應高於和EditProfile是配置文件選項卡中?

回答

1

讓你Profile活動extends ActivityGroup
然後下面的代碼添加到onClick(View v)

View view = getLocalActivityManager().startActivity("EditProfile", 
     new Intent(v.getContext(),EditProfile.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) 
     .getDecorView(); 
setContentView(view); 
1

選項卡中的活動可以通過以下方式進行切換。

首先讓我們來了解流程:下

  1. 我們有一個接頭主機,活動(比如列表),從中我們需要去到下一個活動(說的詳細點的點擊項)同一個標籤。爲此,我們可以使用替換活動的概念。同時設置所選標籤的標誌和其他知道現在正在顯示的細節的標誌

  2. 當我們按回時,我們應該在同一個標​​簽下獲得先前的活動。對於此,而不是再次更換活動,我們可以同時使用其中被選定爲標籤的特定標誌刷新選項卡。此外,如果標誌顯示的細節是真實的,我們會去的名單在同一個標​​籤,否則我們將tabwidget(正常使用onBackPressed的)之前去活動

的代碼可以如下..

  1. 對於從列表中的細節去...

(這可以在onClickListener)

private OnClickListener textListener = new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     Constants.SHOW_DETAILS = true; 
     Intent intent = new Intent(context, DetailsActivity.class); 
     replaceContentView("activity3", intent); 
     } 
}; 

public void replaceContentView(String id, Intent newIntent) { 
    View view = ((ActivityGroup) context) 
      .getLocalActivityManager() 
      .startActivity(id, 
        newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) 
      .getDecorView(); 
    ((Activity) context).setContentView(view); 

} 
  1. 當回壓一點,我們在每一個活動的覆蓋上BackPressed選項卡下再去列表從細節屏幕

    @Override 
        public void onBackPressed() { 
    // TODO Auto-generated method stub 
    super.onBackPressed(); 
    if (MathHelper.SHOW_DETAILS) { 
        Log.e("back", "pressed accepted"); 
        Constants.LIST_ACTIVITY = 1; 
        Constants.SHOW_DETAILS = false; 
        Intent intent = new Intent(this, Tab_widget.class); 
        startActivity(intent); 
        finish(); 
        } 
    } 
    

這裏最重要的部分是 常量.LIST_ACTIVITY = 1;它表明我們是在哪個標籤。所以相應的活動將其作爲0,1,2值...等

的標籤活動被刷新時再加載正確的列表(Activty),我們必須包括這在TabWidget的onCreate創建標籤後

tabHost.setCurrentTab(Constants.LIST_ACTIVITY);