码迷,mamicode.com
首页 > 其他好文 > 详细

RadioGroup+TabHost

时间:2015-12-08 14:23:49      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

=.=

 

//MainActivity

public class MainActivity extends TabActivity implements
		OnCheckedChangeListener {

	private static long firstTime;

	private TabHost tabHost;
	private RadioGroup radioderGroup;

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
				&& event.getAction() == KeyEvent.ACTION_DOWN) {

			if (firstTime + 2000 > System.currentTimeMillis()) {
				super.onBackPressed();
			} else {
				ToastUtils.showShort(this, "再按一次退出程序");
			}
			firstTime = System.currentTimeMillis();

			return false;
		}
		return super.dispatchKeyEvent(event);
	}

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);

		setContentView(R.layout.activity_new_main);

		tabHost = this.getTabHost();
		tabHost.addTab(tabHost.newTabSpec("首页").setIndicator("首页")
				.setContent(new Intent(this, MainWebActivity.class)));
		tabHost.addTab(tabHost.newTabSpec("消息").setIndicator("消息")
				.setContent(new Intent(this, MessageActivity.class)));
		tabHost.addTab(tabHost.newTabSpec("发现").setIndicator("发现")
				.setContent(new Intent(this, FindWebActivity.class)));
		// tabHost.addTab(tabHost.newTabSpec("个人").setIndicator("个人").setContent(new
		// Intent(this, UserCenterWebActivity.class)));

		tabHost.addTab(tabHost.newTabSpec("个人").setIndicator("个人")
				.setContent(new Intent(this, MyProfileActivity.class)));

		radioderGroup = (RadioGroup) findViewById(R.id.main_radio);
		radioderGroup.setOnCheckedChangeListener(this);
		radioderGroup.check(R.id.mainTabs_radio_home);// 默认第一个按钮

		EventBus.getDefault().register(this);
	}

	public void onEventMainThread(MainEvent event) {

		switch (event.what) {
		case 3:

			PrefUtils.putString(this, "find_web_index", "2");
			radioderGroup.check(R.id.mainTabs_radio_find);

			break;
		case 4:

			PrefUtils.putString(this, "find_web_index", "3");

			radioderGroup.check(R.id.mainTabs_radio_find);
			break;

		default:
			break;
		}

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		EventBus.getDefault().unregister(this);
		super.onDestroy();
	}

	/**
	 * 连续按两次返回键就退出
	 */
	@Override
	public void onBackPressed() {

	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {
		case R.id.mainTabs_radio_home:
			tabHost.setCurrentTabByTag("首页");
			break;
		case R.id.mainTabs_radio_message:
			tabHost.setCurrentTabByTag("消息");
			break;
		case R.id.mainTabs_radio_find:
			tabHost.setCurrentTabByTag("发现");
			break;
		case R.id.mainTabs_radio_personage:
			tabHost.setCurrentTabByTag("个人");
			break;

		}
	}

}

 

//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="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        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" />

        <include layout="@layout/include_divider" />

        <RadioGroup
            android:id="@+id/main_radio"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingBottom="3dp"
            android:paddingTop="3dp" >

            <RadioButton
                android:id="@+id/mainTabs_radio_home"
                style="@style/mainTabs_radio"
                android:drawableTop="@drawable/bg_below_home_selector"
                android:text="首页" />

            <RadioButton
                android:id="@+id/mainTabs_radio_message"
                style="@style/mainTabs_radio"
                android:drawableTop="@drawable/bg_below_message_selector"
                android:text="消息" />

            <RadioButton
                android:id="@+id/mainTabs_radio_find"
                style="@style/mainTabs_radio"
                android:drawableTop="@drawable/bg_below_find_selector"
                android:text="发现" />

            <RadioButton
                android:id="@+id/mainTabs_radio_personage"
                style="@style/mainTabs_radio"
                android:drawableTop="@drawable/bg_below_personage_selector"
                android:text="个人" />
        </RadioGroup>
    </LinearLayout>

</TabHost>

  

 

RadioGroup+TabHost

标签:

原文地址:http://www.cnblogs.com/wikiki/p/5028922.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!