android tabhost

結構html

繼承關係java

public class TabHost extends FrameLayout implements ViewTreeObserver.OnTouchModeChangeListenerandroid

java.lang.Objectapp

android.view.View函數

android.view.ViewGroup佈局

android.widget.FrameLayoutthis

android.widget.TabHostspa

 提供選項卡(Tab頁)的窗口視圖容器。此對象包含兩個子對象:一組是用戶能夠選擇指定Tab頁的標籤;另外一組是FrameLayout用來顯示該Tab頁的內容。個別元素一般控制使用這個容器對象,而不是設置在子元素自己的值。.net

(譯者注:即便使用的是單個元素,也最好把它放到容器對象ViewGroup裏)code

 內部類

interface TabHost.OnTabChangeListener

接口定義了當選項卡更改時被調用的回調函數

interface TabHost.TabContentFactory

當某一選項卡被選中時生成選項卡的內容

class TabHost.TabSpec

單獨的選項卡,每一個選項卡都有一個選項卡指示符,內容和tag標籤,以便於記錄.

公共方法

public void addTab (TabHost.TabSpec tabSpec)

新增一個選項卡

參數

tabSpec 指定怎樣建立指示符和內容.

public void clearAllTabs ()

tab widget中移除全部關聯到當前tab host的選項卡

public boolean dispatchKeyEvent (KeyEvent event)

分發按鍵事件到焦點傳遞路線上的下一視圖。焦點傳遞路線從視圖樹的頂層開始一直到當前獲取焦點的視圖中止。若是此視圖已經獲取焦點,將分發給它自身。不然,將分發到焦點傳遞路線的下一節點。此方法會觸發任何一個按鍵監聽器.

(譯者注:關於focus path,能夠參考如下地址:

http://blog.csdn.net/maxleng/archive/2010/05/04/5557758.aspx)

參數

event 分發的按鍵事件

返回值

若是事件已經處理則返回true,不然返回false.

public void dispatchWindowFocusChanged (boolean hasFocus)

當窗口包含的此視圖獲取或丟失焦點時觸發此方法.ViewGroups應該重寫以路由到他的子元素

參數

hasFocus 若是窗口包含的此view依獲取焦點,返回true,不然返回false.

public int getCurrentTab ()

(譯者注:獲取當前選項卡的id

public String getCurrentTabTag ()

(譯者注:當前選項卡的Tag標籤內容)

public View getCurrentTabView ()

(譯者注:獲取當前選項卡的視圖view

public View getCurrentView ()

(譯者注:獲取當前的視圖view

public FrameLayout getTabContentView ()

獲取保存tab內容的FrameLayout

public TabWidget getTabWidget ()

(譯者注:根據系統規定的idtabs來找到TabWidget,並返回,注意,這裏的ID必須是tabs。源代碼中表示以下:

private TabWidget mTabWidget;

mTabWidget=(TabWidget)findViewById(com.android.internal.R.id.tabs);

public TabHost.TabSpec newTabSpec (String tag)

獲取一個新的TabHost.TabSpec,並關聯到當前tab host

參數

tag 所需的選項卡標籤(tag)

public void onTouchModeChanged (boolean isInTouchMode)

當觸摸模式發生改變時調用的回調函數.

參數

isInTouchMode 若是視圖結構當前處於觸摸模式,返回true,不然返回false.

public void setCurrentTab (int index)

(譯者注:設置當前的選項卡

參數

Index 爲當前選項卡的索引。)

public void setCurrentTabByTag (String tag)

(譯者注:根據選項卡的Tab標籤來設置當前的選項卡

參數

tag 想要被設置爲當前選項卡的tag標籤值。)

public void setOnTabChangedListener (TabHost.OnTabChangeListener l)

註冊一個回調函數,當選項卡中的任何一個tab的選中狀態發生改變時調用。

(譯者注:setCurrentTab(index)時會觸發調用)

參數

l 將運行的回調函數

public void setup ()

若是使用findViewById()加載TabHost,那麼在新增一個選項卡tab以前,須要調用setup()。然而,當你在一個TabActivity裏使用getTabHost()獲取TabHost,你就再也不須要調用setup()了。(譯者注:實現tab窗口的兩種方法:繼承activity時,使用findViewById()查找TabHost,而後調用setup();繼承TabActivity,經過getTabHost()查找,此時不用調用setup())例子:

public void setup (LocalActivityManager activityGroup)

若是你使用setContent(android.content.Intent),那麼當activityGroup用於加載本地activity之時,必須調用此方法。若是你拓展(繼承)TabActivity將自動調用setup()方法。

參數

activityGroup 用來爲選項卡內容加載activitiesativityGroup

受保護方法

protected void onAttachedToWindow ()

當視圖附加到窗口上時被調用。在這個點的表面進行繪製。注意此函數確保在onDraw(Cancas)以前調用,然而它可能在第一次執行onDraw以前的任什麼時候間被調用——包括的onMeasure(int,int)的以前或以後。

protected void onDetachedFromWindow ()

當視圖從窗口分離時被調用。在這個點的表面再也不有畫面繪製。


 ============================================================================

 TabHost普遍運用於android程序中,在程序中運用TabHost,解決了手機屏幕小,顯示內容少的問題,如系統自帶的撥號程序,就用了TabHost

 

分爲撥號、通話紀錄、聯繫人、收藏幾個Tab。

定義佈局文件:tabhost.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp">
         
      <LinearLayout 
		android:id="@+id/tab_edit"
		android:layout_width="fill_parent" 
		android:layout_height="fill_parent"
		androidrientation="vertical">
		<EditText 
			android:id="@+id/edit" 
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" 
			android:text="請輸入檢索關鍵字..."
			android:textSize="18sp"/>
		<Button 
			android:id="@+id/but" 
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" 
			android:text="搜索"/>
	</LinearLayout>
	<LinearLayout 
		android:id="@+id/tab_clock"
		android:layout_width="fill_parent" 
		android:layout_height="fill_parent"
		androidrientation="vertical">
		<AnalogClock 
			android:id="@+id/myAnalogClock"
			android:layout_width="wrap_content" 
			android:layout_height="wrap_content"/>
	</LinearLayout>
	<LinearLayout 
		android:id="@+id/tab_sex"
		android:layout_width="fill_parent" 
		android:layout_height="fill_parent"
		androidrientation="vertical">
		<RadioGroup
			android:id="@+id/sex"
			android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			android:orientation="vertical">
			<RadioButton
				android:id="@+id/male"
				android:checked="true"
				android:text="性別:男"/>
			<RadioButton
				android:id="@+id/female"
				android:text="性別:女"/>
		</RadioGroup>
	</LinearLayout>  
     </LinearLayout>
</ScrollView>

2.java代碼:

package com.test;

import java.util.ArrayList;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.test.R;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.ToggleButton;

public class TabHostDemo extends TabActivity {
	//自定義
	private  TabHost  mytabhost = null;  // 定義TabHost
	private int[] layRes = { R.id.tab_edit, R.id.tab_clock , R.id.tab_sex };	
	
	
	
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mytabhost = super.getTabHost() ;			// 取得TabHost對象
		LayoutInflater.from(this) 						// 取得LayoutInflater對象
		.inflate(R.layout.tabhost,					// 定義轉換的佈局管理器 
				this.mytabhost.getTabContentView(),	// 指定標籤增長的容器
				true); 							// 實例化佈局管理器中的組件
		for (int x = 0; x < this.layRes.length; x++) {	// 循環取出全部佈局標記
			TabSpec myTab = mytabhost.newTabSpec("tab" + x);	// 定義TabSpec
			myTab.setIndicator("標籤 - " + x) ;			// 設置標籤文字
			myTab.setContent(this.layRes[x]) ;			// 設置顯示的組件
			this.mytabhost.addTab(myTab) ;				// 增長標籤
		}
		
	}
}

3.執行結果:

=========================================================================

案例二:

http://www.eoeandroid.com/thread-1035-1-1.html

相關文章
相關標籤/搜索