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

  1. 项目开发中经常遇到使用底部弹出Dialog 例如选择图片上传时的 本地图库 和 拍照  还有支付时的支付方式选择  
  2. 下面是我写的一个支付方式选择的底部弹出 Dialog  不足之处请指出 谢谢
Android 底部弹出Dialog(支付方式选择)_第1张图片
Android 底部弹出Dialog(支付方式选择)_第2张图片
  • 自定义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 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);
        }
    }



  • 布局文件
  • 
    
    
        
    
            
    
            
        
    
        
    
        
    
        
    
        
    
    
            
    
            
    
                
    
                
            
    
            
        
    
        
    
        
    
    
            
    
            
        
    
        
    
        
    
            
    
            
    
        
    
        
    
        
    
            
    
            
    
        
    
        
    
    

     
  • 初始化方法
  •  /**
         * 初始化支付方式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
  • 
    
    


  • Animtation style
  • 
    


  • animation set
  • 
    
        
    
    
    
    
        
    
    






你可能感兴趣的:(Android)