安卓初級教程(5):TabHost的思考

 package com.myhost;

 import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.app.TabActivity; public class TabhostActivity extends TabActivity { // Extend TabActivity class /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TabHost tabHost = this.getTabHost(); // The activity TabHost
LayoutInflater.from(this).inflate(R.layout.main,tabHost.getTabContentView(), true); tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("tab1", getResources()
.getDrawable(R.drawable.chartareaup_green))
.setContent(R.id.linearLayout1)); tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("tab2", getResources()
.getDrawable(R.drawable.home_yellow))
.setContent(R.id.linearLayout2)); tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("tab3", getResources()
.getDrawable(R.drawable.harddrivedownload_red))
.setContent(R.id.linearLayout3)); tabHost.addTab(tabHost.newTabSpec("tab4")
.setIndicator("tab4", getResources()
.getDrawable(R.drawable.shoppingcart_blue))
.setContent(R.id.linearLayout4));
}
}
 package net.weibo.ui;

 import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec; public class MainActivity extends TabActivity {
/** Called when the activity is first created. */
public TabHost mth;
public static final String TAB_HOME="HOME";
public static final String TAB_MSG="INFORMATION";
public static final String TAB_ABT="ABOUT";
public static final String TAB_SEH="SEARCH";
public static final String TAB_MORE="MORE"; public RadioGroup radioGroup; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); mth=this.getTabHost(); //mth 等於上一段第18行的寫法。 TabSpec ts1=mth.newTabSpec(TAB_HOME).setIndicator(TAB_HOME);
ts1.setContent(new Intent(MainActivity.this,FirstActivity.class));
mth.addTab(ts1); TabSpec ts2=mth.newTabSpec(TAB_MSG).setIndicator(TAB_MSG);
ts2.setContent(new Intent(MainActivity.this,SecondActivity.class));
mth.addTab(ts2); TabSpec ts3=mth.newTabSpec(TAB_ABT).setIndicator(TAB_ABT);
ts3.setContent(new Intent(MainActivity.this,ThirdActivity.class));
mth.addTab(ts3); TabSpec ts4=mth.newTabSpec(TAB_SEH).setIndicator(TAB_SEH);
ts4.setContent(new Intent(MainActivity.this,ForthActivity.class));
mth.addTab(ts4); TabSpec ts5=mth.newTabSpec(TAB_MORE).setIndicator(TAB_MORE);
ts5.setContent(new Intent(MainActivity.this,FifthActivity.class));
mth.addTab(ts5); this.radioGroup=(RadioGroup)findViewById(R.id.main_radio);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub switch(checkedId){
case R.id.radio_button0: mth.setCurrentTabByTag(TAB_HOME);
break;
case R.id.radio_button1: mth.setCurrentTabByTag(TAB_MSG);
break;
case R.id.radio_button2:
mth.setCurrentTabByTag(TAB_ABT);
break;
case R.id.radio_button3: mth.setCurrentTabByTag(TAB_SEH);
break;
case R.id.radio_button4: mth.setCurrentTabByTag(TAB_MORE);
break;
}
}
}); }
}

以上兩種寫法熟優熟劣?

上一篇:poi实现将数据输出到Excel表格当中


下一篇:java字节中的基本类型的职业的数目 (采访总是问)