tabHost的简单使用以及registerReceiver和Handler的使用

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6


tabHost有点像listView 也有TabActivity或者activity发动

那么看一下Activity发动

class SelfContainedTabHost extends Activity
setContentView(R.layout.tabhost_container);
        
        TabHost tabs = (TabHost)this.findViewById(R.id.tabhost);
        tabs.setup();
        
      
       
        tabs.addTab(tabs.newTabSpec("one").setContent(R.id.tab1content).setIndicator("TAB 1"));
        tabs.addTab(tabs.newTabSpec("two").setContent(R.id.tab2content).setIndicator("TAB 2"));
        tabs.setCurrentTab(0);

 前两部是必须的,在xml中也需要一个

<TabWidget
    		android:id="@android:id/tabs"
        	android:layout_width="fill_parent"
        	android:layout_height="60px"
        
        	android:paddingBottom="10dip"/>

 

注意他的id android:id/tabs。

整个xml为

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TabHost
    	android:id="@+id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
  
  <TabWidget
    		android:id="@android:id/tabs"
        	android:layout_width="fill_parent"
        	android:layout_height="60px"
        
        	android:paddingBottom="10dip"/>
    	
    	<FrameLayout
    		android:id="@android:id/tabcontent"
        	android:layout_width="fill_parent"
        	android:layout_height="fill_parent"
        	android:paddingTop="60px">


			<LinearLayout
				android:id="@+id/tab1content"
				android:orientation="vertical"
				android:layout_width="fill_parent"
				android:layout_height="fill_parent"
				android:background="#00008B">
			
				<!--  Put Tab 1 Views in here -->
				
			</LinearLayout>

			<LinearLayout
				android:id="@+id/tab2content"
				android:orientation="vertical"
				android:layout_width="fill_parent"
				android:layout_height="fill_parent"
				android:background="#FFFF00">

				<!--  Put Tab 2 Views in here -->
				
			</LinearLayout>
			
			
		</FrameLayout>
		
	</TabHost>			
</LinearLayout>

 

2.tabActivity

public class Container extends TabActivity{

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		host = getTabHost();
		Intent intent = new Intent(this, Tab1Activity.class);
		host.addTab(host.newTabSpec("one").setIndicator("TAB1").setContent(intent));
		intent = new Intent(this, Tab2Activity.class);
		host.addTab(host.newTabSpec("two").setIndicator("TAB2").setContent(intent));
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		receiver = new TabChangeReceiver();
		registerReceiver(receiver, new IntentFilter("com.novoda.TAB"), null, mHandler);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		unregisterReceiver(receiver);
	};
	
    protected Handler mHandler = new Handler() {
        @Override public void handleMessage(Message msg) {
            switch (msg.what) {
            	case SWITCH_TAB:
            		Log.i("handler", "using the handler");
                    host.setCurrentTab(msg.arg1);            		
            		break;
            }
        }
    };

    public class TabChangeReceiver extends android.content.BroadcastReceiver{
		@Override
		public void onReceive(Context context, Intent intent) {
			int intExtra = intent.getIntExtra("tab", 0);
			Log.i(TabChangeReceiver.class.getSimpleName(), "Recieved broadcast with extra=[" + intExtra + "]");
			
			mHandler.sendMessage(mHandler.obtainMessage(SWITCH_TAB, intExtra, 0));
		}
	}
	
	private TabHost host;
	public static final int SWITCH_TAB = 2545;
	protected static TabChangeReceiver receiver;
	
}

 

public class Tab1Activity extends Activity{
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.tab1);
        findViewById(R.id.tab1button).setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent("com.novoda.TAB");
				intent.putExtra("tab", 1);
				sendBroadcast(intent);
			}
		});
	}
}

 

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

“tabHost的简单使用以及registerReceiver和Handler的使用” 的相关文章

面板数据缺失填补-stata实现线性插值法过程

目录 一、原始数据说明 二、代码及过程解释 1.设定面板数据 2.内插法填补数据 3.外插法填补数据 三、注意事项 线性插值是指插值函数为一次多项式的插值方式其在插值节点上的插值误差为零。线性插值相比其他插值方式如抛物线插值具有简单、方便的特点。线性插值的几何意义即为概述图中利用过A...

流黄稠鼻涕是什么感冒

风热感冒是风热之邪犯表、肺气失和所致。症状表现为发热重、微恶风、头胀痛、有汗、咽喉红肿疼痛、咳嗽、痰粘或黄、鼻塞黄涕、口渴喜饮、舌尖边红、苔薄白微黄。治法应以辛凉解表为主。常选用菊花、薄荷、桑叶等。《银翘散》、《桑菊饮》。服成药可选用银翘解毒丸(片)、羚翘解毒丸、桑菊感冒片、板兰根冲剂等。如发热...

C语言课程设计题目介绍(10个标准题目)

《C语言课程设计》 1、学生成绩管理系统 学生数据由学号、姓名、班级、三门课数学、英语、计算机的成绩和平均成绩构成。 实现功能包括 1添加学生的记录 2查询学生分别按学号和姓名 3对学生数据排序分别按平均成绩和计算机成绩的降序 4删除学生记录 5修改学生记录 6班级成绩分析各...

UVa 490 Rotating Sentences (water ver.)

490 - Rotating SentencesTime limit: 3.000 secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=sh...

UVa 10300 Ecological Premium (water ver.)

10300 - Ecological Premium Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&pag...

adb fastboot 模式开启流程

Android 的 fastboot 模式 主要是解锁一些系统只读目录的写权限例如在单编一些系统级App导入设备验证一些效果的时候无法正常安装使用fastboot模式可以节省很多时间或者一些其他配置文件等的东西都可以快速导入替换 开启步骤 手机系统 打开开发者模式开发者选项 --打开OE...