展示负反馈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>