浅谈Android切换卡TabWidget应用

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

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

浅谈Android切换卡TabWidget应用

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

布局文件:

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"> 
        <TabWidget 
            android:id="@android:id/tabs" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" /> 
        <FrameLayout 
            android:id="@android:id/tabcontent" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"> 
            <TextView 
                android:id="@+id/textview1" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent" 
                android:text="this is a tab" /> 
            <TextView 
                android:id="@+id/textview2" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent" 
                android:text="this is another tab" /> 
            <TextView 
                android:id="@+id/textview3" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent" 
                android:text="this is a third tab" /> 
        </FrameLayout> 
    </LinearLayout> 
</TabHost> 
 
源代码:  
package com.yarin.android.TestOnWidget;  
 
import android.app.AlertDialog;  
import android.app.Dialog;  
import android.app.TabActivity;  
import android.content.DialogInterface;  
import android.graphics.Color;  
import android.os.Bundle;  
import android.widget.TabHost;  
import android.widget.TabHost.OnTabChangeListener;  
 
public class mytestWidget extends TabActivity  
{  
    //声明TabHost对象  
    TabHost mTabHost;  
     
    @Override  
    public void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
         
        //取得TabHost对象  
        mTabHost = getTabHost();  
         
         
        //新建一个newTabSpec(newTabSpec)  
        //设置其标签和图标(setIndicator)  
        //设置内容(setContent)  
        mTabHost.addTab(mTabHost.newTabSpec("tab_test1")  
                .setIndicator("TAB 1",getResources().getDrawable(R.drawable.img1))  
                .setContent(R.id.textview1));  
        mTabHost.addTab(mTabHost.newTabSpec("tab_test2")  
                .setIndicator("TAB 2",getResources().getDrawable(R.drawable.img2))  
                .setContent(R.id.textview2));  
        mTabHost.addTab(mTabHost.newTabSpec("tab_test3")  
                .setIndicator("TAB 3",getResources().getDrawable(R.drawable.img3))  
                .setContent(R.id.textview3));  
         
        //设置TabHost的背景颜色  
        mTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));  
        //设置TabHost的背景图片资源  
        //mTabHost.setBackgroundResource(R.drawable.bg0);  
         
        //设置当前显示哪一个标签  
        mTabHost.setCurrentTab(0);  
         
        //标签切换事件处理,setOnTabChangedListener  
        mTabHost.setOnTabChangedListener(new OnTabChangeListener()  
        {  
            // TODO Auto-generated method stub  
            @Override  
            public void onTabChanged(String tabId)  
            {  
                    Dialog dialog = new AlertDialog.Builder(mytestWidget.this)  
                            .setTitle("提示")  
                            .setMessage("当前选中:"+tabId+"标签")  
                            .setPositiveButton("确定",  
                            new DialogInterface.OnClickListener()  
                            {  
                                public void onClick(DialogInterface dialog, int whichButton)  
                                {  
                                    dialog.cancel();  
                                }  
                            }).create();//创建按钮  
               
                    dialog.show();  
            }             
        });  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.

小结:浅谈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-09-01 16:46:02

DHCP Relay

2010-06-12 17:28:35

协议封装

2011-09-08 17:48:33

Web Widget

2009-07-20 15:30:11

ASP.NET应用

2022-09-08 09:59:23

API网络安全

2010-06-03 11:39:33

2023-08-06 07:05:25

Android优化

2010-01-26 17:42:14

Android浮点

2011-08-31 13:27:52

AndroidPhoneGap

2009-06-24 17:05:10

2009-07-08 09:32:25

Java设计模式

2010-09-29 16:38:03

企业应用访问

2023-12-17 14:36:05

2011-09-07 16:36:00

Qt Widget

2013-05-23 10:51:28

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

2009-08-06 10:27:08

C#应用程序域

2009-07-21 15:02:19

ASP.NET应用程序
点赞
收藏

51CTO技术栈公众号