2011-02-04 49 views
0

我有一個使用Intents填充內容視圖的TabActivity類。在某些情況下,我想攔截選項卡選擇事件,建立消息對話框,禁止選定的意圖,並恢復到選定的原始選項卡。Android:在填充內容視圖之前執行檢查的TabActivity

我想讓TabActivity內容保持Intent驅動(而不是使用視圖)。

我懷疑這可能需要擴展LocalActivityManager。

有沒有人完成過這個或做過類似的事情?

// simple example of current code: 

TabHost tabHost = getTabHost(); 
TabSpec ts = tabHost.newTabSpec(tag); 
ts.setIndicator(tabview); 
ts.setContent(new Intent().setClass(this, AHome.class)); 
tabHost.addTab(ts); 

謝謝!

+0

「我希望TabActivity內容保持由Intent驅動(而不是使用視圖)」 - 爲什麼? – CommonsWare 2011-02-04 01:56:19

+0

@CommonsWare:因爲我想讓內容視圖包含一個活動而不是視圖。這些活動已經針對其內容進行了特定構建。他們本質上是MVC中的控制器。 – paiego 2011-02-04 06:18:09

回答

0

我不會在TabActivity中尋找答案(甚至Google員工也承認這個API已損壞)。 這是我做的 - 在目標活動中,我會在onCreate中檢查這個條件,如果條件滿足,繼續,如果沒有 - 激活之前的活動

0

稍微深入Android的TabHost src之後,一個相當簡單的解決方案。它允許以圖形方式「觸摸」選項卡按鈕,但仍然保持未選中狀態,並且阻止對選定選項卡進行任何處理(假定所有OnTabSelected偵聽器都已知曉)。

只是擴展TabHost類:

public class MyTabHost extends TabHost 
{ 
    public MyTabHost(Context context) 
    { 
     super(context); 
    } 

    public MyTabHost(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
    } 

    public void setCurrentTab(int index) 
    { 
     // e.g. substitute ? with the tab index(s) for which to perform a check. 
     if (index == ?) 
     { 
      if (/* a block condition exists */) 
      { 
       // Perform any pre-checking before allowing final tab selection 
       Toast.makeText(this.getContext(), "msg", Toast.LENGTH_SHORT).show(); 
       return; 
      } 
     } 
     super.setCurrentTab(index); 
    } 
} 

然後從改變你參考TabHostMyTabHost在用於TabActivity的XML:

<com.hos.MyTabHost 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 

    <LinearLayout  
     android:id="@+id/llTest" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:padding="0dp" 
     > 

    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="0dp" 
     android:layout_gravity="top" 
     android:layout_weight="1" 
     /> 

    <TabWidget 
     android:id="@android:id/tabs" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom"    
     android:layout_weight="0" 
     /> 

    </LinearLayout> 

</com.hos.MyTabHost> 

一兩件事要記住如果您在TabActivity中使用TabActivity.getTabHost(),它將返回一個MyTabHost。例如:

MyTabHost mth = (MyTabHost)getTabHost(); 
相關問題