TabHost的学习!

这几天一直在玩Tab,方法也有很多种,但是下面这个很实用,特记录下来了!
 
package exp.tableHost;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

public class HelloTabWidget extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	 super.onCreate(savedInstanceState);
    	    setContentView(R.layout.main);

    	    Resources res = getResources(); // Resource object to get Drawables
    	    TabHost tabHost = getTabHost();  // The activity TabHost
    	    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    	    Intent intent;  // Reusable Intent for each tab

    	    // Create an Intent to launch an Activity for the tab (to be reused)
    	    intent = new Intent().setClass(this, AddActivity.class);

    	    // Initialize a TabSpec for each tab and add it to the TabHost
    	    spec = tabHost.newTabSpec("add").setIndicator("添加",
    	                      res.getDrawable(R.drawable.add))
    	                  .setContent(intent);
    	    tabHost.addTab(spec);

    	    // Do the same for the other tabs
    	    intent = new Intent().setClass(this, QueryActivity.class);
    	    spec = tabHost.newTabSpec("query").setIndicator("查询",
    	                      res.getDrawable(R.drawable.query))
    	                  .setContent(intent);
    	    tabHost.addTab(spec);

    	    intent = new Intent().setClass(this, CalculateActivity.class);
    	    spec = tabHost.newTabSpec("calculate").setIndicator("计算",
    	                      res.getDrawable(R.drawable.calculate))
    	                  .setContent(intent);
    	    tabHost.addTab(spec);

    	    intent = new Intent().setClass(this, SetActivity.class);

    	    // Initialize a TabSpec for each tab and add it to the TabHost
    	    spec = tabHost.newTabSpec("about").setIndicator("关于",
    	                      res.getDrawable(R.drawable.about))
    	                  .setContent(intent);
    	    tabHost.addTab(spec);

    	    tabHost.setCurrentTab(2);
    	    //处理tabId
    	    tabHost.setOnTabChangedListener(new OnTabChangeListener() {
				public void onTabChanged(String tabId) {
                         Log.i("HelloTabWidget", tabId) ;
				}
			});

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值