TabRadioDemo实现tabhost与RadioBox的结合

来源:互联网 发布:linux中echo n 编辑:程序博客网 时间:2024/04/28 12:12

     这篇文章中我们实现了tabhost和RadioBox的结合,现在的好多android的应用都使用这样的方法,具体实现的代码如下:

首先呢先看一下布局

tab_radio.xml

<?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" ><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_group"            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/main_rb_home"                style="@style/main_tab_bottom"                android:background="@drawable/main_rb_selector"                android:drawableTop="@drawable/rb_home_selector"                android:text="主页"             android:textColor="@color/grey"                android:checked="true" />            <RadioButton                android:id="@+id/main_rb_search"                style="@style/main_tab_bottom"                android:background="@drawable/main_rb_selector"                android:drawableTop="@drawable/rb_search_selector"                android:text="收索"                android:textColor="@color/grey" />            <RadioButton                android:id="@+id/main_rb_camera"                style="@style/main_tab_bottom"                android:background="@drawable/main_rb_selector"                android:drawableTop="@drawable/rb_share_selector"                android:text="图片"                android:textColor="@color/grey"/>                    <RadioButton                android:id="@+id/main_rb_selfinfo"                style="@style/main_tab_bottom"                android:background="@drawable/main_rb_selector"                android:drawableTop="@drawable/rb_selfinfo_selector"                android:text="日志"               android:textColor="@color/grey" />            <RadioButton                android:id="@+id/main_rb_more"                style="@style/main_tab_bottom"                android:background="@drawable/main_rb_selector"                android:drawableTop="@drawable/rb_more_selector"                android:text="更多"            android:textColor="@color/grey"/>        </RadioGroup>    </LinearLayout></TabHost>

search_activity.xml 、home_activity.xml、more_activity.xml、notes_activity.xml、photos_activity.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="收索" /></LinearLayout>


然后呢,是动态实现的方法:

package com.nyist.activity;import android.app.Activity;import android.app.TabActivity;import android.content.Intent;import android.os.Bundle;import android.widget.CompoundButton;import android.widget.RadioButton;import android.widget.TabHost;import android.widget.CompoundButton.OnCheckedChangeListener;public class TabRadioDemoActivity extends TabActivity implementsOnCheckedChangeListener {private static final String TAG = "TabRadioDemoActivity";private RadioButton rb_home, rb_search, rb_camera, rb_selfinfo, rb_more;private Intent homeIntent, searchIntent, cameraIntent, selfinfoIntent,moreIntent;private TabHost mTabHost;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.tab_radio);homeIntent = new Intent(TabRadioDemoActivity.this, HomeActivity.class);searchIntent = new Intent(TabRadioDemoActivity.this,SearchActivity.class);cameraIntent = new Intent(TabRadioDemoActivity.this, PhotoesActivity.class);selfinfoIntent = new Intent(TabRadioDemoActivity.this,NotesActivity.class);moreIntent = new Intent(TabRadioDemoActivity.this, MoreActivity.class);setupIntent();findView();}private void setupIntent() {mTabHost = getTabHost();TabHost mainTabHost = this.mTabHost;mainTabHost.addTab(buildTabSpec("home", R.string.main_rb_home,this.homeIntent));mainTabHost.addTab(buildTabSpec("search", R.string.main_rb_search,this.searchIntent));mainTabHost.addTab(buildTabSpec("photoes", R.string.main_rb_camera,this.cameraIntent));mainTabHost.addTab(buildTabSpec("notes", R.string.main_rb_selfinfo,this.selfinfoIntent));mainTabHost.addTab(buildTabSpec("more", R.string.main_rb_more,this.moreIntent));mainTabHost.setCurrentTab(0);}private void findView() {rb_home = (RadioButton) this.findViewById(R.id.main_rb_home);rb_search = (RadioButton) this.findViewById(R.id.main_rb_search);rb_camera = (RadioButton) this.findViewById(R.id.main_rb_camera);rb_selfinfo = (RadioButton) this.findViewById(R.id.main_rb_selfinfo);rb_more = (RadioButton) this.findViewById(R.id.main_rb_more);rb_home.setOnCheckedChangeListener(this);rb_search.setOnCheckedChangeListener(this);rb_camera.setOnCheckedChangeListener(this);rb_selfinfo.setOnCheckedChangeListener(this);rb_more.setOnCheckedChangeListener(this);}private TabHost.TabSpec buildTabSpec(String tag, int resLabel,final Intent content) {return this.mTabHost.newTabSpec(tag).setIndicator(getString(resLabel)).setContent(content);}@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked) {switch (buttonView.getId()) {case R.id.main_rb_home:mTabHost.setCurrentTabByTag("home");break;case R.id.main_rb_search:mTabHost.setCurrentTabByTag("search");break;case R.id.main_rb_camera:mTabHost.setCurrentTabByTag("photoes");break;case R.id.main_rb_selfinfo:mTabHost.setCurrentTabByTag("notes");break;case R.id.main_rb_more:mTabHost.setCurrentTabByTag("more");}}}}
还有search_activity.xml 、home_activity.xml、more_activity.xml、notes_activity.xml、photos_activity.xml所对应的Activity。
package com.nyist.activity;import android.app.Activity;import android.os.Bundle;public class SearchActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.search_activity);}}
ps:最后呢,不要忘记注册Activity哦!!

运行结果如下: