2011-02-04 52 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(); 
相关问题