自定义dialog

该代码展示了一个用于Android应用的负反馈Dialog,用户可通过预设的ChipGroup选择反馈原因,如已签收、已退货等。点击Chip会触发相应事件,点击删除按钮会关闭Dialog并记录所选反馈。
摘要由CSDN通过智能技术生成

展示负反馈dialog

package com.fzw.myapplication.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.fzw.myapplication.R;
import com.google.android.material.chip.Chip;
import com.google.android.material.chip.ChipGroup;

import java.util.Arrays;

/**
 * @Author fzw
 * @Date 2023/4/27
 * @desc 快递负反馈弹框
 */
public class ExpressFeedbackUtil {
    private static final String TAG = "ExpressFeedbackUtil";

    // 展示负反馈dialog
    public static void showFeedbackDialog(Context context) {
        // 1、获取ChipGroup
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View inflate = layoutInflater.inflate(R.layout.activity_express_feedback, null);
        ChipGroup chipGroup = inflate.findViewById(R.id.express_feedback_cg);

        // 2、设置ChipGroup的反馈理由chips
        setChips(context, chipGroup, layoutInflater);

        // 3、 设置chip的点击事件
        setClickEvent(chipGroup);

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("反馈原因");
        builder.setView(inflate);
        builder.setNegativeButton("删除", (dialog, which) -> {
            Chip chip = (Chip) chipGroup.getChildAt(chipGroup.getCheckedChipId() - 1);
            if (chip != null && chip.isChecked()) {
                CharSequence text = chip.getText();
                Log.i(TAG, "click id: " + chipGroup.getCheckedChipId() + ", text: " + text);
            }
            dialog.dismiss();
        });
        builder.show();
    }

    // 动态添加负反馈理由
    private static void setChips(Context context, ChipGroup chipGroup, LayoutInflater layoutInflater) {
        Arrays.stream(context.getResources()
                .getStringArray(R.array.express_feedback_reason)).forEach(reason -> {
            Chip chip = (Chip) layoutInflater.inflate(R.layout.express_feedback_reason_chip, chipGroup, false);
            chip.setText(reason);
            chipGroup.addView(chip);
        });
    }

    private static void setClickEvent(ChipGroup chipGroup) {
        chipGroup.setOnCheckedChangeListener((group, checkedId) -> {
            Chip chip = (Chip) group.getChildAt(checkedId - 1);
            if (chip != null && chip.isChecked()) {
                // 取消按钮置灰
            }
        });
    }
}

获取ChipGroup:activity_express_feedback.xml

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

    <!-- 默认水平分布,自动换行-->
    <com.google.android.material.chip.ChipGroup
        android:id="@+id/express_feedback_cg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:singleSelection="true" >

    </com.google.android.material.chip.ChipGroup>

</LinearLayout>

chip反馈理由:string.xml

    <string-array name="express_feedback_reason">
        <item>我已签收</item>
        <item>我已退货</item>
        <item>不想要此卡片</item>
    </string-array>

express_feedback_reason_chip.xml

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.chip.Chip style="@style/Widget.MaterialComponents.Chip.Choice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checkable="true"
    android:clickable="true"
    app:chipBackgroundColor="@color/express_feedback_chip_bg"
    android:textColor="@color/express_feedback_chip_text_bg"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" />


chip背景express_feedback_chip_bg.xml

<!--?xml version="1.0" encoding="utf-8"?-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/mtrl_choice_chip_background_color" android:state_checked="true"/>
     <item android:color="@color/white" android:state_checked="false"/>
</selector>

chip文字颜色express_feedback_chip_text_bg.xml

<!--?xml version="1.0" encoding="utf-8"?-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/black" android:state_checked="true"/>
    <item android:color="@color/purple_200"/>
</selector>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值