浅谈Android切换卡TabWidget应用

移动开发
Android切换卡TabWidget应用是本文要介绍的内容,本文主要是以代码来讲解TabWidget的案例实现,具体内容的实现来看本文详细代码。

Android切换卡TabWidget应用是本文要介绍的内容,主要是来了解并学习Android Widget的应用,本文主要是以代码来讲解TabWidget的案例实现。先看效果图:

浅谈Android切换卡TabWidget应用

TabWidget继承自TabActivity类,并实现setOnTabChangedListener的onTabChanged方法来监听Tab的改变:

布局文件:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:id="@android:id/tabhost" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent"> 
  6.     <LinearLayout 
  7.         android:orientation="vertical" 
  8.         android:layout_width="fill_parent" 
  9.         android:layout_height="fill_parent"> 
  10.         <TabWidget 
  11.             android:id="@android:id/tabs" 
  12.             android:layout_width="fill_parent" 
  13.             android:layout_height="wrap_content" /> 
  14.         <FrameLayout 
  15.             android:id="@android:id/tabcontent" 
  16.             android:layout_width="fill_parent" 
  17.             android:layout_height="fill_parent"> 
  18.             <TextView 
  19.                 android:id="@+id/textview1" 
  20.                 android:layout_width="fill_parent" 
  21.                 android:layout_height="fill_parent" 
  22.                 android:text="this is a tab" /> 
  23.             <TextView 
  24.                 android:id="@+id/textview2" 
  25.                 android:layout_width="fill_parent" 
  26.                 android:layout_height="fill_parent" 
  27.                 android:text="this is another tab" /> 
  28.             <TextView 
  29.                 android:id="@+id/textview3" 
  30.                 android:layout_width="fill_parent" 
  31.                 android:layout_height="fill_parent" 
  32.                 android:text="this is a third tab" /> 
  33.         </FrameLayout> 
  34.     </LinearLayout> 
  35. </TabHost> 
  36.  
  37. 源代码:  
  38. package com.yarin.android.TestOnWidget;  
  39.  
  40. import android.app.AlertDialog;  
  41. import android.app.Dialog;  
  42. import android.app.TabActivity;  
  43. import android.content.DialogInterface;  
  44. import android.graphics.Color;  
  45. import android.os.Bundle;  
  46. import android.widget.TabHost;  
  47. import android.widget.TabHost.OnTabChangeListener;  
  48.  
  49. public class mytestWidget extends TabActivity  
  50. {  
  51.     //声明TabHost对象  
  52.     TabHost mTabHost;  
  53.      
  54.     @Override  
  55.     public void onCreate(Bundle savedInstanceState)  
  56.     {  
  57.         super.onCreate(savedInstanceState);  
  58.         setContentView(R.layout.main);  
  59.          
  60.         //取得TabHost对象  
  61.         mTabHost = getTabHost();  
  62.          
  63.          
  64.         //新建一个newTabSpec(newTabSpec)  
  65.         //设置其标签和图标(setIndicator)  
  66.         //设置内容(setContent)  
  67.         mTabHost.addTab(mTabHost.newTabSpec("tab_test1")  
  68.                 .setIndicator("TAB 1",getResources().getDrawable(R.drawable.img1))  
  69.                 .setContent(R.id.textview1));  
  70.         mTabHost.addTab(mTabHost.newTabSpec("tab_test2")  
  71.                 .setIndicator("TAB 2",getResources().getDrawable(R.drawable.img2))  
  72.                 .setContent(R.id.textview2));  
  73.         mTabHost.addTab(mTabHost.newTabSpec("tab_test3")  
  74.                 .setIndicator("TAB 3",getResources().getDrawable(R.drawable.img3))  
  75.                 .setContent(R.id.textview3));  
  76.          
  77.         //设置TabHost的背景颜色  
  78.         mTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));  
  79.         //设置TabHost的背景图片资源  
  80.         //mTabHost.setBackgroundResource(R.drawable.bg0);  
  81.          
  82.         //设置当前显示哪一个标签  
  83.         mTabHost.setCurrentTab(0);  
  84.          
  85.         //标签切换事件处理,setOnTabChangedListener  
  86.         mTabHost.setOnTabChangedListener(new OnTabChangeListener()  
  87.         {  
  88.             // TODO Auto-generated method stub  
  89.             @Override  
  90.             public void onTabChanged(String tabId)  
  91.             {  
  92.                     Dialog dialog = new AlertDialog.Builder(mytestWidget.this)  
  93.                             .setTitle("提示")  
  94.                             .setMessage("当前选中:"+tabId+"标签")  
  95.                             .setPositiveButton("确定",  
  96.                             new DialogInterface.OnClickListener()  
  97.                             {  
  98.                                 public void onClick(DialogInterface dialog, int whichButton)  
  99.                                 {  
  100.                                     dialog.cancel();  
  101.                                 }  
  102.                             }).create();//创建按钮  
  103.                
  104.                     dialog.show();  
  105.             }             
  106.         });  
  107.     }  

小结:浅谈Android切换卡TabWidget应用的内容介绍完了,希望通过Android Widget中TabWidget案例的内容能对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-09-07 13:30:48

Android WidTabWidget

2009-09-03 16:52:28

C#回车切换焦点

2022-04-20 10:23:15

GoogleiPhone转移Android设备

2010-06-03 11:39:33

2022-09-08 09:59:23

API网络安全

2011-09-08 17:48:33

Web Widget

2009-07-20 15:30:11

ASP.NET应用

2010-06-12 17:28:35

协议封装

2010-09-01 16:46:02

DHCP Relay

2013-05-23 10:51:28

Android开发移动开发横竖屏切换

2023-08-06 07:05:25

Android优化

2010-09-29 16:38:03

企业应用访问

2011-09-07 16:36:00

Qt Widget

2023-12-17 14:36:05

2011-08-31 13:27:52

AndroidPhoneGap

2010-01-26 17:42:14

Android浮点

2009-07-08 09:32:25

Java设计模式

2009-06-24 17:05:10

2009-07-14 11:08:42

WebRendererSwing应用程序

2009-07-01 13:54:03

JSP注释
点赞
收藏

51CTO技术栈公众号