【Android -- Material Design】MaterialButtonToggleGroup 的基本使用

不断学习,做更好的自己!💪

视频号CSDN简书
欢迎打开微信,关注我的视频号:程序员朵朵点我点我

前言

官网:MaterialButtonToggleGroup

继承关系

23.png

属性

24.png

效果图

01.png

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <com.google.android.material.button.MaterialButtonToggleGroup
        android:id="@+id/toggle_group"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginLeft="@dimen/DIMEN_12dp"
        android:layout_marginRight="@dimen/DIMEN_12dp"
        app:checkedButton="@id/mbtn_news"
        app:singleSelection="true"
        app:selectionRequired="true">

        <com.google.android.material.button.MaterialButton
            android:id="@+id/mbtn_news"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            app:cornerRadius="10dp"
            android:text="新闻"/>

        <com.google.android.material.button.MaterialButton
            android:id="@+id/mbtn_funs"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            android:text="娱乐"/>

        <com.google.android.material.button.MaterialButton
            android:id="@+id/mbtn_images"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            app:cornerRadius="10dp"
            android:text="图片"/>
    </com.google.android.material.button.MaterialButtonToggleGroup>

    <com.google.android.material.button.MaterialButtonToggleGroup
        android:id="@+id/toggleGroup2"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/DIMEN_60dp"
        android:layout_margin="@dimen/DIMEN_12dp"
        app:checkedButton="@id/btnA"
        app:singleSelection="true">

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btnA"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Android"
            app:icon="@android:drawable/star_on" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btnS"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Sunny"
            app:icon="@android:drawable/ic_lock_lock" />

    </com.google.android.material.button.MaterialButtonToggleGroup>

    <com.google.android.material.button.MaterialButtonToggleGroup
        android:id="@+id/toggle_group2"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginLeft="@dimen/DIMEN_12dp"
        android:layout_marginRight="@dimen/DIMEN_12dp"
        app:singleSelection="false">
        <com.google.android.material.button.MaterialButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            app:cornerRadius="10dp"
            android:text="新闻"/>

        <com.google.android.material.button.MaterialButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            android:text="娱乐"/>

        <com.google.android.material.button.MaterialButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            app:cornerRadius="10dp"
            android:text="图片"/>
    </com.google.android.material.button.MaterialButtonToggleGroup>
</LinearLayout>

逻辑代码

public class MainActivity extends AppCompatActivity {
    private MaterialButtonToggleGroup mToggleGroup;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        initView();
        initEvent();

    }

    private void initView() {
        mToggleGroup = findViewById(R.id.toggle_group);
    }

    private void initEvent() {
        mToggleGroup.addOnButtonCheckedListener(new MaterialButtonToggleGroup.OnButtonCheckedListener() {
            @Override
            public void onButtonChecked(MaterialButtonToggleGroup group, int checkedId, boolean isChecked) {
                switch (checkedId) {
                    case R.id.mbtn_news:
                        Toast.makeText(MainActivity.this,"clicked new",Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.mbtn_funs:
                        Toast.makeText(MainActivity.this,"clicked fun",Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.mbtn_images:
                        Toast.makeText(MainActivity.this,"clicked image",Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        break;
                }
            }
        });
    }
}
### 回答1: Android开发中使用Kotlin语言开发Material Design项目可以带来很多好处。Kotlin是一种现代化的编程语言,它可以提高开发效率、减少代码量、提高代码可读性和可维护性。同时,Material Design是一种现代化的设计语言,它可以提高用户体验、提高应用的可用性和可访问性。因此,使用Kotlin开发Material Design项目可以使应用更加现代化、高效、易于维护和易于使用。 ### 回答2: 随着移动设备的迅速普及,Android操作系统已经成为全球最流行的移动操作系统之一。这使得Android应用程序的开发变得越来越受欢迎,许多开发人员也开始学习和掌握这个平台。随着时间的推移,开发人员也在不断寻找最好的解决方案来创建优秀的应用程序,其中kotlin和material design就是两种最受欢迎的选择。 Kotlin是一种高级编程语言,它是Java虚拟机的官方语言之一。Kotlin在Android开发中的流行程度日益增加,因为它具有许多特性,如可空类型、lambdas、扩展函数等,使得开发Android应用程序更加便捷和高效。Kotlin支持Java虚拟机,并非Android特定的开发语言,因此具有更广泛的用途,可以与其他语言无缝集成。同时,Kotlin还有很多实用特性,如可空类型、类型推断、lambda表达式等,能够在Android开发中大幅提高开发效率,在代码中减少了很多荣誉的代码和冗长表达式的麻烦。 Material Design是一种设计语言,由Google推出,旨在为移动和Web应用程序提供一致的极致体验。Material Design提供了一系列的设计指南、模式和组件,以便开发人员可以为他们的应用程序在不同平台和设备上提供一致的体验,从而使应用程序更加具有现代感和吸引力。 Material DesignAndroid开发者提供了一些标准的界面组件,如浮动操作按钮、抽屉式导航等,同时也支持进行自定义设计,为应用程序增加独特的特色。 综合考虑,使用Kotlin和Material Design组合开发Android应用程序可以有很多好处。Kotlin可以使代码更加简洁,同时使用Material Design的组件和元素可以使应用程序显得更加现代和美观。 这种组合还可以提高开发效率,减少代码中的bug,在Android平台上提供更好的用户体验,从而为应用程序的成功打下坚实的基础。总的来说,采用这种开发方式的应用程序将具有更高的可维护性和可扩展性,也将在市场上拥有更高的竞争力。 ### 回答3: 在当前的移动应用开发市场中,Android系统已经成为了主流之一,其开发工具也被越来越多的开发者采用。而Kotlin语言作为一种新兴的编程语言,因其简洁、安全、互通性和易用性等特点,在Android系统开发中越来越受到开发者的欢迎。 在这种情况下,开发Material Design项目需要用到的技术和工具也应该是非常有趣的。 Android系统采用Material Design作为其UI设计风格,提供了一套完备的UI组件库。要开发Material Design的应用程序,需要遵循Google的Material Design规范以及使用相关的Android开发API和工具。Kotlin语言提供了很多的便利,在使用Android开发API和工具的同时,还提供了方便的语法和Lambda表达式。 使用Kotlin开发Material Design项目还可以提高应用程序的安全性,避免因类型不安全、空指针等问题导致的错误。Kotlin还支持函数式编程,可以提供更好的编写UI代码的方法,帮助开发人员高效编写代码,提高开发效率。 Kotlin增加了一些与Java不同的特性,使得开发者能够更快、更方便地编写代码。例如,Kotlin具有空安全机制,可帮助开发者尽早发现和解决可能导致应用程序崩溃的问题,从而提高应用程序的质量。Kotlin还提供了lambda表达式、扩展功能和集合操作,更方便开发人员在项目中增加新特性,缩短应用开发周期。 虽然Kotlin开发Material Design项目相对于Java来说还是一个相对新的领域,但随着Kotlin使用率的逐渐增加,越来越多的开发者正在使用和探索Kotlin在Android开发中的应用。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kevin-Dev

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值