为了账号安全,请及时绑定邮箱和手机立即绑定

RadioButton 切换按钮 做类似QQ 消息与电话

标签:
Android

直接上图

5bba1b39000132a604500860.jpg

类似QQ 中 消息与电话按钮切换:

记得使用RadioButton 做切换按钮的时候一定要 嵌套在RadioGroup 里面,布局文件如下

[代码]xml代码:

?

01

02

03

04

05

06

07

08

09

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="#DCDCDC"

    android:orientation="vertical">

 

    <RelativeLayout

        android:id="@+id/rl"

        android:layout_width="match_parent"

        android:layout_height="45dp"

        android:layout_marginBottom="10dp"

        android:layout_marginTop="10dp">

 

        <RadioGroup

            android:id="@+id/select"

            android:layout_width="200dp"

            android:layout_height="match_parent"

            android:layout_centerInParent="true"

            android:orientation="horizontal">

 

            <RadioButton

                android:id="@+id/dialUp"

                android:layout_width="match_parent"

                android:layout_height="match_parent"

                android:layout_gravity="center_vertical"

                android:layout_weight="1"

                android:background="@drawable/radiobutton_left_bg"

                android:button="@null"

                android:checked="true"

                android:gravity="center"

                android:text="拨号"

                android:textColor="@drawable/text_select" />

 

            <RadioButton

                android:id="@+id/contacts"

                android:layout_width="match_parent"

                android:layout_height="match_parent"

                android:layout_gravity="center_vertical"

                android:layout_weight="1"

                android:background="@drawable/radiobutton_right_bg"

                android:button="@null"

                android:gravity="center"

                android:text="联系人"

                android:textColor="@drawable/text_select" />

        </RadioGroup>

    </RelativeLayout>

 

    <View

        android:id="@+id/view"

        android:layout_width="match_parent"

        android:layout_height="2px"

        android:layout_below="@+id/rl"

        android:background="#D3D3D3" />

 

    <FrameLayout

        android:id="@+id/franme_meun"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_below="@+id/view"

        android:background="#FFFFFF" />

</RelativeLayout>

 

代码如下:

[代码]java代码:

?

01

02

03

04

05

06

07

08

09

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

public class HomeActivity   extends AppCompatActivity   {

    private RadioGroup mSelect;

    //拨号

    private RadioButton mDialUp;

    //联系人

    private RadioButton mContacts;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_home);

 

        findView();

    }

 

    private void findView() {

 

        mDialUp   = (RadioButton) findViewById(R.id.dialUp);

        mContacts   = (RadioButton) findViewById(R.id.contacts);

        mSelect   = (RadioGroup) findViewById(R.id.select);

        mSelect.setOnCheckedChangeListener(listener);

 

        //   设置默认显示页面

        Fragment   dialUp = new DialUpFragment();

        FragmentManager   fmangerhp = getFragmentManager();

        FragmentTransaction   ftranhp = fmangerhp.beginTransaction();

        ftranhp.replace(R.id.franme_meun,   dialUp);

        ftranhp.commit();

 

    }

 

    private RadioGroup.OnCheckedChangeListener   listener = new RadioGroup.OnCheckedChangeListener()   {

 

        @Override

        public void onCheckedChanged(RadioGroup group, int checkedId) {

            //   TODO Auto-generated method stub

            switch (checkedId) {

                case R.id.dialUp:

                    //   这里是你想要跳转的界面

                    Fragment   dialUp = new DialUpFragment();

                    FragmentManager   fmangerhp = getFragmentManager();

                    FragmentTransaction   ftranhp = fmangerhp.beginTransaction();

                    ftranhp.replace(R.id.franme_meun,   dialUp);

                    ftranhp.commit();

                    mDialUp.setChecked(true);

                    break;

 

                case R.id.contacts:

                    //   这里是你想要跳转的界面

                    Fragment   contacts = new ContactsFragment();

                    FragmentManager   sanManager = getFragmentManager();

                    FragmentTransaction   sanfTransaction = sanManager.beginTransaction();

                    sanfTransaction.replace(R.id.franme_meun,   contacts);

                    sanfTransaction.commit();

 

                    break;

 

                default:

                    break;

            }

        }

 

    };

}

原文链接:http://www.apkbus.com/blog-455907-61209.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消