android使用TabHost实现tabbar,包括监听事件

/**   
* @Title: GatewayActivity.java 
* @Package com.yisa.qiqilogin.activity 
* @Description: TODO(用一句话描述该文件做什么) 
* @author hq  
* @date 2015年1月8日 下午1:53:44 
* @version V1.0   
*/
package com.yisa.qiqilogin.activity;

import com.yisa.qiqilogin.R;
import com.yisa.qiqilogin.service.NotificationService;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;

/** 
 * @ClassName: GatewayActivity 
 * @Description: 门户页
 * @author hq 
 * @date 2015年1月8日 下午1:53:44 
 *  
 */
public class GatewayActivity extends TabActivity {
	private TabHost tabHost;
	private TabWidget mTabWidget;
	
	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Intent intent = getIntent();
		setContentView(R.layout.layout_gateway);
		String userName = intent.getStringExtra("username");
 		//启动通知的service
 		Intent notiSevrIntent = new Intent(this, NotificationService.class);
 		startService(notiSevrIntent);
 		
 		
        //获取TabHost对象
// 		Intent intent2 = new Intent(GatewayActivity.this,  NoticeCenter.class);
        tabHost = getTabHost();  
        mTabWidget = tabHost.getTabWidget(); 
        
//        tabHost.setup();
        //新建一个newTabSpec,设置标签和图标(setIndicator),设置内容(setContent)
        tabHost.addTab(tabHost.newTabSpec("homepage").setIndicator("",getResources().getDrawable(R.drawable.homenormal)).setContent(R.id.tabFirst));
        tabHost.addTab(tabHost.newTabSpec("userinfo").setIndicator("",getResources().getDrawable(R.drawable.categorynormal)).setContent(R.id.tabSecond));
        tabHost.addTab(tabHost.newTabSpec("noticecenter").setIndicator("",getResources().getDrawable(R.drawable.personnormal)).setContent(R.id.lastTab));
        //设置TabHost的背景颜色
        tabHost.setBackgroundColor(Color.argb(150,22,70,150));
        //设置TabHost的背景图片资源
//      tabHost.setBackgroundResource(R.drawable.bg);

        View v = mTabWidget.getChildTabViewAt(2);
        v.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(GatewayActivity.this, NoticeCenter.class);
        		startActivity(intent);
			}
        	
        });
        //设置当前现实哪一个标签
        tabHost.setCurrentTab(0);   //0为标签ID
        //标签切换处理,用setOnTabChangedListener  
        tabHost.setOnTabChangedListener(new OnTabChangeListener(){
        	@Override
            public void onTabChanged(String tabId){
            	Log.d("GatewayActivity--tabId--=", tabId);
//                Toast.makeText(TabTestActivity.this, "This is a Test!", Toast.LENGTH_LONG).show();
//            	if("noticecenter".equals(tabId)){
//            		Log.d("GatewayActivity--success--=", tabId);
//            		Intent intent = new Intent(GatewayActivity.this, NoticeCenter.class);
//            		startActivity(intent);
//            	}
            }
        });
	}
}


    
	    
	    
	    
	        
	        
	            
	        
	        
	            
	            
	        
	        
	        
   		
    


你可能感兴趣的:(android)