TabSpec和TabHost实例

TabSpec与TabHost

TabHost相当于浏览器中浏览器分布的集合,而Tabspec则相当于浏览器中的 每一个分页面。d在Android中,每一个TabSpec分布可以是一个组件,也可以是一个布局,然后将每一个分页装入TabHost 中,TabHost即可将其中的每一个分页一并显示出来。

步骤:

(1)继承TabActivity:在此之前继承的都是android.app.Activity类,但是这里需要继承android.app.TabActivity。

(2)创建TabHost分布菜单对象,利用以下代码。

LayoutInflater.from(this).inflate(R.layout.main,tableHost.getTabContentView());

(3)实例化实分页

java代码:

[html] view plain copy
  1. package com.test;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.TabActivity;  
  5. import android.os.Bundle;  
  6. import android.view.LayoutInflater;  
  7. import android.widget.TabHost;  
  8. import android.widget.TabHost.OnTabChangeListener;  
  9. import android.widget.Toast;  
  10. import android.widget.TabHost.TabSpec;  
  11.   
  12. public class MainActivity extends TabActivity implements OnTabChangeListener {  
  13.     /** Called when the activity is first created. */  
  14.     private TabSpec ts1 ,ts2, ts3 ;//声明3个分页  
  15.     private TabHost tableHost;//分页菜单(tab的容器)  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         tableHost  =  this .getTabHost();//实例 (分页)菜单  
  20.         //利用LayoutInflater将布局与分页菜单一起显示  
  21.         LayoutInflater.from(this).inflate(R.layout.main, tableHost.getTabContentView());  
  22.         ts1  =  tableHost .newTabSpec("tabOne");//实例化一个分页  
  23.         ts1.setIndicator("Tab1");//设置此分页显示的标题  
  24.         ts1.setContent(R.id.btn);//设置此分页的资源id  
  25.         ts2 = tableHost .newTabSpec("tabTwo");//实例化第二个分页  
  26.         //设置此分页显示的标题和图标  
  27.         ts2.setIndicator("Tab2",getResources().getDrawable(R.drawable.icon));  
  28.         ts2.setContent(R.id.et);  
  29.         ts3 tableHost .newTabSpec("tabThree");//实例化第三个分页  
  30.         ts3.setIndicator("Tab3");  
  31.         ts3.setContent(R.id.mylayout);//设置此分页的布局id  
  32.         tableHost.addTab(ts1);//菜单中添加ts1分页  
  33.         tableHost.addTab(ts2);  
  34.         tableHost.addTab(ts3);  
  35.         tableHost.setOnTabChangedListener(this);         
  36.     }  
  37.     public void onTabChanged(String tabId){  
  38.         if(tabId.equals("tabOne")){  
  39.             Toast.makeText(this, "分页1", Toast.LENGTH_LONG).show();  
  40.         }  
  41.         if(tabId.equals("tabTwo")){  
  42.             Toast.makeText(this, "分页2", Toast.LENGTH_LONG).show();  
  43.         }  
  44.         if(tabId.equals("tabThree")){  
  45.             Toast.makeText(this, "分页3", Toast.LENGTH_LONG).show();  
  46.         }  
  47.     }  
  48. }  


布局代码:

[html] view plain copy
  1. <? xml   version = "1.0"   encoding = "utf-8" ?>   
  2. < LinearLayout   xmlns:android = "http://schemas.android.com/apk/res/android"   
  3.     android:layout_width = "fill_parent"   
  4.     android:layout_height = "fill_parent"   
  5.     android:orientation = "vertical"   >   
  6.   
  7.     < Button   
  8.         android:id = "@+id/btn"   
  9.         android:layout_width = "fill_parent"   
  10.         android:layout_height = "fill_parent"   
  11.         android:text = "This is Tab1"   
  12.         />   
  13.     < EditText   
  14.         android:id = "@+id/et"   
  15.         android:layout_width = "fill_parent"   
  16.         android:layout_height = "wrap_content"   
  17.         android:text = "This is Tab2"   
  18.         />   
  19.     < LinearLayout   xmlns:android = "http://schemas.android.com/apk/res/android"   
  20.         android:orientation = "vertical"   
  21.         android:layout_width = "fill_parent"   
  22.         android:layout_height = "fill_parent"   
  23.         android:id = "@+id/mylayout"   
  24.         android:background = "@drawable/bg"   
  25.         >   
  26.          < Button    
  27.              android:layout_width = "fill_parent"   
  28.              android:layout_height = "wrap_content"   
  29.              android:text = "This is Tab3"   
  30.              />   
  31.          < EditText   
  32.              android:layout_width = "fill_parent"   
  33.              android:layout_height = "wrap_content"   
  34.              android:text = "This is Tab3"   
  35.              />   
  36.     </ LinearLayout >   
  37. </ LinearLayout >   


运行结果:

扫描二维码关注公众号,回复: 768691 查看本文章

总结:监听分页改变事件,具体如下:

1、使用OnTabChangeListener接口,重写OnTabChanged(String tabId)函数

2、TabHost绑定监听器

3、判断OnTabChanged(String tabId)中的tabId参数进行处理事件;这里的tabId对应的是实例中每个分页传入的分页ID,而不是TabSpec.setIndicatior()设置的标题

转:http://blog.csdn.net/myserverthepeople/article/details/7630052

猜你喜欢

转载自qsh123.iteye.com/blog/1698256