.net

.net

博客园 首页 新随笔 联系 订阅 管理
 

给tabhost加上点击监听,不是onTabChanged(String)监听

 5209人阅读 评论(0) 收藏 举报

   最近在项目中遇到点击tabhost里的tab项时做出监听,注意不是onTabChanged(String str)的监听,因为该监听是只有当你切换tab时才会发生动作事件。

   TabHost的使用:

1:首先定义tabhost的布局

  1.  <TabHost  
  2.             android:id="@android:id/tabhost"  
  3.             android:layout_width="fill_parent"  
  4.             android:layout_height="fill_parent"  
  5.              >    
  6.   
  7. <LinearLayout  
  8.                 android:layout_width="fill_parent"  
  9.                 android:layout_height="wrap_content"  
  10.                 android:orientation="vertical" >  
  11.   
  12. <TabWidget  
  13.                         android:id="@android:id/tabs"  
  14.                         android:layout_width="wrap_content"  
  15.                         android:layout_height="50dp"  
  16.                          />  
  17.   
  18. FrameLayout  
  19.                     android:id="@android:id/tabcontent"  
  20.                     android:layout_width="fill_parent"  
  21.                     android:layout_height="fill_parent"  
  22.                     android:background="@android:color/transparent"  
  23.                    />  
  24.   
  25.   </LinearLayout>  
  26.   
  27.   </TabHost>  


这样你有了布局,

接下来,让你的类继承TabActivity,然后定义成员变量:

  1. private TabHost mTabHost;  
  2.   
  3. private TabWidget mTabWidget;  

接着在OnCreate()里面进行初始化,

  1. mTabHost = getTabHost();  
  2. mTabHost.setup(getLocalActivityManager());  
  3. mTabWidget = mTabHost.getTabWidget();  


然后进行setTab1(),setTab2()······

  1. mTabHost.setOnTabChangedListener(this);  
  2.        
  3. setTab(TAB_1, true);  
  4. //默认设置第一个选项卡  

setTab1()方法就是创建新的标签tab代码如下:

 

  1. private void setTab1() {  
  2.   View view = mInflater.inflate(R.layout.xxx, null);  
  3.   ((TextView) view.findViewById(R.id.x)).setText(getResources()  
  4.     .getString(R.string.tab_forum_1));  
  5.   Intent newsList = new Intent(this, AAAivity.class);  
  6.   TabSpec mTabSpec1 = mTabHost.newTabSpec(getResources().getString(  
  7.     R.string.tab_forum_1));  
  8.   mTabSpec1.setIndicator(view);  
  9.   mTabSpec1.setContent(newsList);  
  10.   mTabHost.addTab(mTabSpec1);  
  11.  }  

setTab(TAB_1,true);方法其中里面的TAB_1=1;

该方法为:监听测试为点击第二个tab选项卡时出现点击事件。

  1. private void setTab(int id, boolean flag) {  
  2.   switch (id) {  
  3.   case TAB_1:  
  4.   
  5.    mTabWidget.getChildAt(TAB_1).setBackgroundResource(R.drawable.select_group_bg2);  
  6.   
  7.   abWidget.getChildAt(TAB_2).setBackgroundDrawable(null);  
  8.   
  9. case TAB_2:  
  10.    mTabWidget.getChildAt(TAB_2).setOnClickListener(new OnClickListener() {     
  11.     @Override  
  12.     public void onClick(View v) {  
  13.       if (mTabHost.getCurrentTab() != TAB_2) {//一定要判断这个是为了防止阻碍切换事件  
  14.                          mTabHost.setCurrentTab(TAB_2);  
  15.                      }else{  
  16.   
  17.                      //做你要做的事情  
  18.                        }  
  19.     }  
  20.    });  
  21.    mTabWidget.getChildAt(TAB_1).setBackgroundDrawable(null);  
  22.   
  23. }  

好了,接着写onTabChanged(String tabId)方法

  

  1.  if (tabId.equalsIgnoreCase(getResources().getString(  
  2.     R.string.tab_forum_1))) {  
  3.    setTab(TAB_1, true);  
  4.   } else if (tabId.equalsIgnoreCase(getResources().getString(  
  5.     R.string.tab_forum_2))) {  
  6.    setTab(TAB_2, true);  
  7.   
  8. }  
  9.   
  10. }  


好了监听已经实现。

posted on 2014-04-13 11:56  航宇  阅读(9682)  评论(0编辑  收藏  举报