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

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);
			}
		});
	}
}

 

你可能感兴趣的:(android,xml)