自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺談Android切換卡TabWidget應(yīng)用

移動開發(fā)
Android切換卡TabWidget應(yīng)用是本文要介紹的內(nèi)容,本文主要是以代碼來講解TabWidget的案例實現(xiàn),具體內(nèi)容的實現(xiàn)來看本文詳細代碼。

Android切換卡TabWidget應(yīng)用是本文要介紹的內(nèi)容,主要是來了解并學(xué)習(xí)Android Widget的應(yīng)用,本文主要是以代碼來講解TabWidget的案例實現(xiàn)。先看效果圖:

淺談Android切換卡TabWidget應(yīng)用

TabWidget繼承自TabActivity類,并實現(xiàn)setOnTabChangedListener的onTabChanged方法來監(jiān)聽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.         //設(shè)置其標簽和圖標(setIndicator)  
  66.         //設(shè)置內(nèi)容(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.         //設(shè)置TabHost的背景顏色  
  78.         mTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));  
  79.         //設(shè)置TabHost的背景圖片資源  
  80.         //mTabHost.setBackgroundResource(R.drawable.bg0);  
  81.          
  82.         //設(shè)置當前顯示哪一個標簽  
  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();//創(chuàng)建按鈕  
  103.                
  104.                     dialog.show();  
  105.             }             
  106.         });  
  107.     }  

小結(jié):淺談Android切換卡TabWidget應(yīng)用的內(nèi)容介紹完了,希望通過Android Widget中TabWidget案例的內(nèi)容能對你有所幫助!

責(zé)任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-09-07 13:30:48

Android WidTabWidget

2009-09-03 16:52:28

C#回車切換焦點

2022-04-20 10:23:15

GoogleiPhone轉(zhuǎn)移Android設(shè)備

2022-09-08 09:59:23

API網(wǎng)絡(luò)安全

2010-06-03 11:39:33

2010-06-12 17:28:35

協(xié)議封裝

2009-07-20 15:30:11

ASP.NET應(yīng)用

2011-09-08 17:48:33

Web Widget

2010-09-01 16:46:02

DHCP Relay

2023-08-06 07:05:25

Android優(yōu)化

2010-09-29 16:38:03

企業(yè)應(yīng)用訪問

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-06-24 17:05:10

2009-07-08 09:32:25

Java設(shè)計模式

2013-05-23 10:51:28

Android開發(fā)移動開發(fā)橫豎屏切換

2009-07-14 11:08:42

WebRendererSwing應(yīng)用程序

2009-03-11 09:33:11

Lotus開發(fā)Workflow
點贊
收藏

51CTO技術(shù)棧公眾號