RadioGroup+TabHost

//MainActivity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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 groupint 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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?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>
posted @ 2016-12-01 11:58  天涯海角路  阅读(112)  评论(0)    收藏  举报