AppCompatDialogFragment自定義模態彈框
阿新 • • 發佈:2018-12-09
很多時候系統自帶的AlertDialog並不能滿足我們模態彈框的需求,我們的app具體業務可能需要定製化的模態彈框,這個時候我們可以通過繼承AppCompatDialogFragment來定製化開發我們需要的模態彈框。廢話少說,直接上程式碼:
1.xml佈局(addfruit_dialog_layout.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" android:gravity="center"> <RelativeLayout android:clickable="false" android:layout_width="550dp" android:layout_height="470dp" android:background="@drawable/addfruitdialogbg"> <ImageView android:id="@+id/iv_logo" android:layout_width="250dp" android:layout_height="250dp" android:layout_alignParentRight="true" android:background="@color/systemNoColor" android:layout_marginTop="50dp" android:layout_marginRight="5dp" android:scaleType="fitXY" android:src="@drawable/productlaoding" /> <TextView android:id="@+id/tv_des" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:layout_marginTop="30dp" android:alpha="0.5" android:text="請將商品放到電子稱上" android:textColor="#000000" android:textSize="18sp" /> <TextView android:id="@+id/tv_addfruit_ProductUnit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_des" android:layout_marginLeft="15dp" android:layout_marginTop="30dp" android:alpha="0.5" android:text="單價(元/千克)" android:textColor="#000000" android:textSize="15sp" /> <TextView android:id="@+id/tv_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_addfruit_ProductUnit" android:layout_marginLeft="15dp" android:text="8.90" android:textColor="#000000" android:textSize="42sp" /> <TextView android:id="@+id/tv_addfruit_weightUnit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_price" android:layout_marginLeft="15dp" android:layout_marginTop="30dp" android:alpha="0.5" android:text="重量" android:textColor="#000000" android:textSize="15sp" /> <TextView android:id="@+id/tv_quality" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_addfruit_weightUnit" android:text="1.58" android:layout_marginLeft="15dp" android:textColor="@color/aliBlue" android:textSize="60sp" /> <View android:id="@+id/line" android:layout_width="match_parent" android:layout_height="5dp" android:layout_marginBottom="10dp" android:layout_below="@id/tv_quality" android:layout_marginLeft="15dp" android:layout_marginRight="5dp" android:layout_marginTop="8dp" android:background="@drawable/addfruit_line" android:layerType="software" /> <TextView android:id="@+id/total" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/line" android:layout_marginLeft="15dp" android:layout_marginTop="8dp" android:alpha="0.5" android:text="小計(元)" android:textColor="#000000" android:textSize="15sp" /> <TextView android:id="@+id/tv_total" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/total" android:layout_marginLeft="15dp" android:text="1.58" android:textColor="#FC5346" android:textSize="42sp" /> <View android:id="@+id/line1" android:layout_width="match_parent" android:layout_height="0.75dp" android:layout_below="@id/tv_total" android:layout_marginTop="10dp" android:background="#CEDADD" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/line1" android:background="@drawable/pay_dialog_button_bg" android:orientation="horizontal"> <Button android:id="@+id/btn_cancel" style="?android:attr/borderlessButtonStyle" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:alpha="0.6" android:background="@null" android:text="取消" android:textColor="#000000" android:textSize="18sp" /> <View android:layout_width="0.75dp" android:layout_height="match_parent" android:background="#CEDADD" /> <Button android:id="@+id/btn_add" style="?android:attr/borderlessButtonStyle" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:alpha="0.6" android:background="@null" android:text="新增" android:textColor="#FC5346" android:textSize="18sp" /> </LinearLayout> </RelativeLayout> </LinearLayout>
2、自定義彈框java類:
package com.msh.mshselfweighing.app; import android.app.Dialog; import android.content.DialogInterface; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatDialogFragment; import android.text.SpannableString; import android.text.Spanned; import android.text.style.ForegroundColorSpan; import android.text.style.StrikethroughSpan; import android.text.style.UnderlineSpan; import android.util.Log; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import com.bumptech.glide.Glide; import com.msh.mshselfweighing.MainApplication; import com.msh.mshselfweighing.R; import com.msh.mshselfweighing.dto.Category.SelfWeightProductCategoryDto; import com.msh.mshselfweighing.dto.Product.SelfWeightProductInfoDto; import com.msh.mshselfweighing.modelinfo.MachineBaseInfo; import java.math.BigDecimal; import java.text.DecimalFormat; /** * 商品模態框 * Author:William(徐威) * Create Time:2018-09-10 */ public class AddProductDialogFragment extends AppCompatDialogFragment implements View.OnClickListener { private Button btnCancel; private Button btnAdd; private TextView tvDes; private TextView tvPrice; private TextView tvQuality; private TextView tvTotal; private ImageView ivLogo; private DecimalFormat decimalFormat = new DecimalFormat("0.00"); private DecimalFormat weightFormat = new DecimalFormat("0.000"); private BigDecimal price; private String name; boolean isShow = false;//防多次點選 private SelfWeightProductInfoDto buyProductModel;//選擇商品物件 private TextView tv_addfruit_ProductUnit; //單價標籤 private TextView tv_addfruit_weightUnit; //重量標頭 //建構函式 public AddProductDialogFragment() { super(); } @Override public void onStart() { super.onStart(); Dialog dialog = getDialog(); dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); dialog.setCanceledOnTouchOutside(true); //dialog.setCancelable(false); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.addfruit_dialog_layout, container, false); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); initView(view); initAction(); //初始化檢視 Bundle bundle = getArguments(); buyProductModel = (SelfWeightProductInfoDto) bundle.getSerializable("productModel"); initData(); } /** * 初始化檢視 * Author:William(徐威) * Create Time:2018-09-11 * * @param view */ private void initView(View view) { btnAdd = view.findViewById(R.id.btn_add); btnCancel = view.findViewById(R.id.btn_cancel); tvDes = view.findViewById(R.id.tv_des); tvPrice = view.findViewById(R.id.tv_price); tvQuality = view.findViewById(R.id.tv_quality); tvTotal = view.findViewById(R.id.tv_total); ivLogo = view.findViewById(R.id.iv_logo); tv_addfruit_ProductUnit = view.findViewById(R.id.tv_addfruit_ProductUnit); tv_addfruit_weightUnit = view.findViewById(R.id.tv_addfruit_weightUnit); tvQuality.setAlpha(0.5f); } /** * 初始化註冊事件 * Author:William(徐威) * Create Time:2018-09-11 */ private void initAction() { btnAdd.setOnClickListener(this); btnCancel.setOnClickListener(this); btnAdd.setEnabled(false); getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() { @Override public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { return true; } }); } /** * 初始化資料 * Author:William(徐威) * Create Time:2018-09-11 */ private void initData() { if (buyProductModel != null) { //會員價和門店價保留2位四捨五入 buyProductModel.setCurrentPrice(buyProductModel.getCurrentPrice().setScale(2, BigDecimal.ROUND_HALF_UP)); buyProductModel.setStockCurrentPrice(buyProductModel.getStockCurrentPrice().setScale(2, BigDecimal.ROUND_HALF_UP)); tv_addfruit_ProductUnit.setText(String.format("單價(元/%s)", buyProductModel.getDenominatedUnit())); tv_addfruit_weightUnit.setText(String.format("計重(%s)", buyProductModel.getDenominatedUnit())); name = buyProductModel.getProductName(); price = buyProductModel.getCurrentPrice(); tvDes.setText(buyProductModel.getProductName()); //價格 String strPrice = String.format("%s/%s", decimalFormat.format(buyProductModel.getStockCurrentPrice()), decimalFormat.format(price)); SpannableString spanPrice = new SpannableString(strPrice); spanPrice.setSpan(new ForegroundColorSpan(Color.parseColor("#c0c0c0")), 0, decimalFormat.format(buyProductModel.getStockCurrentPrice()).length() + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); spanPrice.setSpan(new StrikethroughSpan(), 0, decimalFormat.format(buyProductModel.getStockCurrentPrice()).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); spanPrice.setSpan(new ForegroundColorSpan(Color.parseColor("#333333")), decimalFormat.format(buyProductModel.getStockCurrentPrice()).length() + 1, strPrice.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); tvPrice.setText(spanPrice); BigDecimal totalAmt = (new BigDecimal(1)).multiply(price); BigDecimal totalStoreAmt = (new BigDecimal(1)).multiply(buyProductModel.getStockCurrentPrice()); tvQuality.setText("1"); Glide.with(getActivity()).load(buyProductModel.getImageUrl()) //載入地址 .placeholder(R.drawable.productlaoding) //載入未完成時顯示佔位圖 .error(R.drawable.productloadfailed) .into(ivLogo); buyProductModel.setV_BuyNumKg(new BigDecimal(1)); buyProductModel.setV_BuyNumJin(new BigDecimal(1)); buyProductModel.setV_BuyTotalAmt(price); buyProductModel.setV_BuyMemberTotalAmt(price); if (MainApplication.ProductWeightJin != null && MainApplication.ProductWeightJin.length() > 0 && buyProductModel.isWeight()) { btnAdd.setEnabled(true); tvTotal.setAlpha(1); tvQuality.setAlpha(1); btnAdd.setAlpha(1); btnCancel.setAlpha(1); tvQuality.setText(MainApplication.ProductWeightJin); totalAmt = (new BigDecimal(MainApplication.ProductWeightJin)).multiply(price).setScale(2, BigDecimal.ROUND_HALF_UP); totalStoreAmt = (new BigDecimal(MainApplication.ProductWeightJin)).multiply(buyProductModel.getStockCurrentPrice()).setScale(2, BigDecimal.ROUND_HALF_UP); BigDecimal storePrice = buyProductModel.getStockCurrentPrice(); BigDecimal storeProductAmt = storePrice.multiply(new BigDecimal(MainApplication.ProductWeightJin)).setScale(2, BigDecimal.ROUND_HALF_UP); buyProductModel.setV_BuyNumKg(new BigDecimal(MainApplication.ProductWeight)); buyProductModel.setV_BuyNumJin(new BigDecimal(MainApplication.ProductWeightJin)); buyProductModel.setV_BuyTotalAmt(storeProductAmt); buyProductModel.setV_BuyMemberTotalAmt(storeProductAmt); } //總價 String strTotalAmt = String.format("%s/%s", decimalFormat.format(totalStoreAmt), decimalFormat.format(totalAmt)); SpannableString spanTotalAmt = new SpannableString(strTotalAmt); spanTotalAmt.setSpan(new ForegroundColorSpan(Color.parseColor("#c0c0c0")), 0, decimalFormat.format(totalStoreAmt).length() + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); spanTotalAmt.setSpan(new StrikethroughSpan(), 0, decimalFormat.format(totalStoreAmt).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); spanTotalAmt.setSpan(new ForegroundColorSpan(Color.parseColor("#D90909")), decimalFormat.format(totalStoreAmt).length() + 1, strTotalAmt.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); tvTotal.setText(spanTotalAmt); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_cancel: //取消按鈕 dismiss(); break; case R.id.btn_add: //新增按鈕 if (listener != null) { listener.onAddProduct(buyProductModel); } dismiss(); break; default: break; } } /** * 更新重量 * Author:William(徐威) * Create Time:2018-09-12 */ public void update(int status) { if (buyProductModel != null && (status == 1 || buyProductModel.getV_BuyNumKg().compareTo(new BigDecimal(MainApplication.ProductWeight)) != 0)) { initData(); } } @Override public void show(FragmentManager manager, String tag) { if (isShow) { return; } super.show(manager, tag); isShow = true; } @Override public void dismiss() { super.dismiss(); isShow = false; } private AddListener listener = null; public void setListener(AddListener listener) { this.listener = listener; } /** * 申明監聽介面 * Author:William(徐威) * Create Time:2018-09-12 */ public interface AddListener { //新增商品事件 void onAddProduct(SelfWeightProductInfoDto buyProductModel); } }
3、呼叫:
public static AddProductDialogFragment dialogFragment = null; //新增商品彈框
IndexActivity.dialogFragment = new AddProductDialogFragment(); IndexActivity.dialogFragment.setListener(new AddProductDialogFragment.AddListener() { //重寫新增商品事件 @Override public void onAddProduct(SelfWeightProductInfoDto buyProductModel) { Toast.makeText(mContext,buyProductModel.getProductName(),Toast.LENGTH_LONG).show(); } }); MainApplication.IsWeight = model.isWeight(); Bundle bundle = new Bundle(); bundle.putSerializable("productModel", model); IndexActivity.dialogFragment.setArguments(bundle); IndexActivity.dialogFragment.show(getFragmentManager(), "AddProductDialogFragment");