手记

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

直接上图

类似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

0人推荐
随时随地看视频
慕课网APP