Android 底部弹出Dialog(支付方式选择)

  1. 项目开发中经常遇到使用底部弹出Dialog 例如选择图片上传时的 本地图库 和 拍照  还有支付时的支付方式选择  
  2. 下面是我写的一个支付方式选择的底部弹出 Dialog  不足之处请指出 谢谢


  • 自定义Dialog
  • /**
     * 选择支付方式Dialog
     * Created by Hh on 2017/3/6.
     */
    
    public class PayWayDialog extends Dialog {
        @BindView(R.id.dialog_balance)
        TextView dialogBalance;
    
        @BindView(R.id.dialog_my_wallet)
        LinearLayout dialogMyWallet;
    
        @BindViews({R.id.recharge_wallet_cb, R.id.recharge_zhifubao_cb, R.id.recharge_wechat_cb})
        List<ImageView> checks;
    
        @BindView(R.id.recharge_num)
        TextView rechargeNum;
    
        @BindView(R.id.dialog_confirm_pay)
        TextView dialogConfirmPay;
    
        /** 判断是充值&支付 **/
        private boolean isRecharge;
    
        private View.OnClickListener onClickListener;
    
        /** 区别三种支付方式 0:我的钱包 1:支付宝 2:微信支付 **/
        public static int payWay = Constants.MY_WALLET;
    
        private Context context;
    
        /**
         *  如果ifRecharge 传入true 则是充值,就隐藏掉我的钱包, 否则则显示
         * @param context
         * @param themeResId
         * @param isRecharge
         * @param onClickListener
         */
        public PayWayDialog(Context context, int themeResId, boolean isRecharge, View.OnClickListener onClickListener) {
            super(context, themeResId);
            this.context = context;
            this.isRecharge = isRecharge;
            this.onClickListener = onClickListener;
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.dialog_pay);
            ButterKnife.bind(this);
            if (isRecharge) {
                dialogMyWallet.setVisibility(View.GONE);
                checks.get(1).setVisibility(View.VISIBLE);
                payWay = Constants.ZHI_FU_BAO;
            }
            Window dialogWindow = getWindow();
            dialogWindow.setGravity(Gravity.BOTTOM);
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = AbsListView.LayoutParams.MATCH_PARENT;
            lp.y = 0;//设置Dialog距离底部的距离
            dialogWindow.setAttributes(lp);
            dialogConfirmPay.setOnClickListener(onClickListener);
        }
    
    
        /**
         * 设置充值金额
         * @param num
         */
        public void setRechargeNum(Editable num, Double balance) {
            rechargeNum.setText("¥ " + StringUtil.numberFormat(Double.parseDouble(num.toString())));
            if (!isRecharge) {
                dialogBalance.setText("可用余额:"+ StringUtil.numberFormat(balance));
            }
        }
    
        @OnClick({R.id.recharge_dialog_close, R.id.dialog_my_wallet, R.id.dialog_zhifubao, R.id.dialog_wechat})
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.recharge_dialog_close:
                    dismiss();
                    break;
                case R.id.dialog_my_wallet:
                    checkChanges(0);
                    break;
                case R.id.dialog_zhifubao:
                    checkChanges(1);
                    break;
                case R.id.dialog_wechat:
                    checkChanges(2);
                    break;
            }
        }
    
        /**
         * 改变选中
         */
        private void checkChanges(int index) {
            for (int i = 0; i < 3; i++) {
                if (i != index) {
                    checks.get(i).setVisibility(View.GONE);
                }
            }
            payWay = index;
            checks.get(index).setVisibility(View.VISIBLE);
        }
    }



  • 布局文件
  • <?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="wrap_content"
        android:background="@color/white"
        android:orientation="vertical">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="@dimen/dp_40"
                android:layout_centerInParent="true"
                android:gravity="center"
                android:text="@string/recharge_dialog_detail"
                android:textColor="@color/black"
                android:textSize="@dimen/dp_18" />
    
            <ImageView
                android:id="@+id/recharge_dialog_close"
                android:layout_width="@dimen/dp_40"
                android:layout_height="@dimen/dp_40"
                android:layout_alignParentRight="true"
                android:scaleType="center"
                android:src="@drawable/icon_right" />
        </RelativeLayout>
    
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/line_bg" />
    
        <TextView
            style="@style/recharge_dialog_text"
            android:text="@string/recharge_dialog_choose" />
    
        <View style="@style/recharge_dialog_line_margin" />
    
        <LinearLayout
            android:id="@+id/dialog_my_wallet"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:gravity="center_vertical">
    
    
            <ImageView
                style="@style/recharge_dialog_text"
                android:layout_width="@dimen/dp_30"
                android:src="@drawable/earnings" />
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">
    
                <TextView
                    android:layout_height="0dp"
                    android:layout_weight="3"
                    style="@style/recharge_dialog_text"
                    android:text="@string/recharge_dialog_mywallet" />
    
                <TextView
                    android:id="@+id/dialog_balance"
                    android:layout_weight="2"
                    android:gravity="top"
                    android:layout_height="0dp"
                    android:textSize="10sp"
                    style="@style/recharge_dialog_text" />
            </LinearLayout>
    
            <ImageView
                android:id="@+id/recharge_wallet_cb"
                android:visibility="visible"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="@dimen/dp_25"
                android:src="@drawable/cb_btn_checked" />
        </LinearLayout>
    
        <View style="@style/recharge_dialog_line_margin" />
    
        <RelativeLayout
            android:id="@+id/dialog_zhifubao"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
    
            <TextView
                style="@style/recharge_dialog_text"
                android:drawableLeft="@drawable/zhifubao"
    
                android:text="@string/recharge_dialog_zhifubao" />
    
            <ImageView
                android:id="@+id/recharge_zhifubao_cb"
                android:layout_width="wrap_content"
                android:visibility="gone"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="@dimen/dp_25"
                android:src="@drawable/cb_btn_checked" />
        </RelativeLayout>
    
        <View style="@style/recharge_dialog_line_margin" />
    
        <RelativeLayout
            android:id="@+id/dialog_wechat"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
                style="@style/recharge_dialog_text"
                android:drawableLeft="@drawable/wechat"
                android:text="@string/recharge_dialog_wechat" />
    
            <ImageView
                android:id="@+id/recharge_wechat_cb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="@dimen/dp_25"
                android:src="@drawable/cb_btn_checked"
                android:visibility="gone" />
    
        </RelativeLayout>
    
        <View style="@style/recharge_dialog_line_margin" />
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:gravity="center_vertical"
            android:minWidth="130dp"
            android:paddingBottom="@dimen/dp_5"
            android:paddingTop="@dimen/dp_5">
    
            <TextView
                style="@style/recharge_dialog_text"
                android:text="@string/recharge_dialog_need" />
    
            <TextView
                android:id="@+id/recharge_num"
                android:layout_width="wrap_content"
                android:layout_height="@dimen/dp_45"
                android:layout_marginLeft="@dimen/dp_8"
                android:layout_marginRight="@dimen/dp_25"
                android:gravity="center"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/text_size_18" />
    
        </LinearLayout>
    
        <TextView
            android:id="@+id/dialog_confirm_pay"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@color/colorPrimary"
            android:gravity="center"
            android:text="@string/recharge_dialog_confirm"
            android:textColor="@color/white"
            android:textSize="@dimen/text_size_16" />
    </LinearLayout>
    

    
    
  • 初始化方法
  •  /**
         * 初始化支付方式Dialog
         */
        private void initDialog() {
            // 隐藏输入法
            SoftInputUtils.hideSoftInput(mActivity);
            dialog = new PayWayDialog(this, R.style.recharge_pay_dialog, true, v -> {
                //确认充值
                ToastUtils.showShortToastSafe(mActivity, "支付方式" + dialog.payWay + "--" + rechargeMoney.getText());
            });
            dialog.show();
            dialog.setRechargeNum(rechargeMoney.getText(),900.0);
        }


  • Dialog Style
  • <!-- 支付方式Dialog 样式 -->
    <style name="recharge_pay_dialog" parent="@android:style/Theme.Dialog">
        <!-- 背景透明 -->
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <!-- 浮于Activity之上 -->
        <item name="android:windowIsFloating">true</item>
        <!-- 边框 -->
        <item name="android:windowFrame">@null</item>
        <!-- Dialog以外的区域模糊效果 -->
        <item name="android:backgroundDimEnabled">true</item>
        <!-- 无标题 -->
        <item name="android:windowNoTitle">true</item>
        <!-- 半透明 -->
        <item name="android:windowIsTranslucent">true</item>
        <!-- Dialog进入及退出动画 -->
        <item name="android:windowAnimationStyle">@style/racharge_dialog_animation</item>
    </style>
    


  • Animtation style
  • <style name="racharge_dialog_animation" parent="@android:style/Animation.Dialog">
        <item name="android:windowEnterAnimation">@anim/dialog_enter</item>
        <item name="android:windowExitAnimation">@anim/dialog_exit</item>
    </style>
    


  • animation set
  • <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:fromYDelta="100%p"  android:duration="400"/>
    </set>
    
    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:toYDelta="100%p"      android:duration="400"/>
    </set>
    






发布了34 篇原创文章 · 获赞 4 · 访问量 3万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览