我从不说废话,直接上代码。
[list]
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <include layout="@layout/activity_title" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0.0dip" android:layout_weight="1.0" /> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.0" android:visibility="gone" /> <RadioGroup android:id="@+id/main_radio" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:gravity="center_vertical" android:orientation="horizontal" > <RadioButton android:id="@+id/radio_btn0" style="@style/main_tab_bottom" android:checked="true" android:drawableTop="@drawable/drawable_home" android:selectAllOnFocus="false" android:tag="radio_btn0" android:text="@string/btn0" /> <RadioButton android:id="@+id/radio_btn1" style="@style/main_tab_bottom" android:drawableTop="@drawable/drawable_news" android:tag="radio_btn1" android:text="@string/btn1" /> <RadioButton android:id="@+id/radio_btn2" style="@style/main_tab_bottom" android:drawableTop="@drawable/drawable_notice" android:tag="radio_btn2" android:text="@string/btn2" /> <RadioButton android:id="@+id/radio_btn3" style="@style/main_tab_bottom" android:drawableTop="@drawable/drawable_topic" android:tag="radio_btn3" android:text="@string/btn3" /> <RadioButton android:id="@+id/radio_btn4" style="@style/main_tab_bottom" android:drawableTop="@drawable/drawable_exit" android:tag="radio_btn4" android:text="@string/btn4" /> </RadioGroup> </LinearLayout> </TabHost>
package com.example.test; import android.app.AlertDialog; import android.app.TabActivity; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.TabHost; import android.widget.TextView; @SuppressWarnings("deprecation") public class MainActivity extends TabActivity implements OnCheckedChangeListener{ private TabHost mHost = null; private RadioGroup radioGroup = null; private RadioButton currentRadioBtn = null; private RadioButton previousRadioBtn = null; private TextView currentTitle = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); previousRadioBtn = (RadioButton)findViewById(R.id.radio_btn0); mHost = this.getTabHost(); mHost.addTab(mHost.newTabSpec("ONE").setIndicator("ONE").setContent(new Intent(this,OneActivity.class))); mHost.addTab(mHost.newTabSpec("TWO").setIndicator("TWO").setContent(new Intent(this,TwoActivity.class))); mHost.addTab(mHost.newTabSpec("THREE").setIndicator("THREE").setContent(new Intent(this,ThreeActivity.class))); mHost.addTab(mHost.newTabSpec("FOUR").setIndicator("FOUR").setContent(new Intent(this,FourActivity.class))); mHost.addTab(mHost.newTabSpec("FIVE").setIndicator("FIVE").setContent(new Intent(this,FiveActivity.class))); radioGroup = (RadioGroup) findViewById(R.id.main_radio); radioGroup.setOnCheckedChangeListener(this); this.getWindow().setBackgroundDrawable(this.getResources().getDrawable(R.drawable.backColor)); } @Override public void onCheckedChanged(RadioGroup group, final int checkedId) { currentRadioBtn = (RadioButton)findViewById(checkedId); currentTitle = (TextView)findViewById(R.id.title); switch(checkedId){ case R.id.radio_btn0: mHost.setCurrentTabByTag("ONE"); currentTitle.setText(R.string.btn0); break; case R.id.radio_btn1: mHost.setCurrentTabByTag("TWO"); currentTitle.setText(R.string.btn1); break; case R.id.radio_btn2: mHost.setCurrentTabByTag("THREE"); currentTitle.setText(R.string.btn2); break; case R.id.radio_btn3: mHost.setCurrentTabByTag("FOUR"); currentTitle.setText(R.string.btn3); break; case R.id.radio_btn4: AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(R.string.confirmExit).setCancelable(false).setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { MainActivity.this.finish(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if(previousRadioBtn!=null){ previousRadioBtn.setChecked(true); } dialog.cancel(); } }); builder.create().show(); break; } if(checkedId!=R.id.radio_btn4){ previousRadioBtn = (RadioButton)findViewById(checkedId); } } }
[/img]
[/list]