仿支付宝输入支付密码2(带动画)

  • 由于用对话框做输入支付密码会出现宽度无法充满,这次我用popwindow来做,功能效果更加容易扩展;这次提供源码噢,有兴趣的可以下载去参考下,带动画噢;
  • 先看看效果图把;
  • 这里写图片描述
  • 主界面只有这么简单,就不多在去介绍了;
        button=(Button)findViewById(R.id.home_button);
        background=(View)findViewById(R.id.home_background);

        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                popWindow = new PayPopWindow(context,background);
                popWindow.showAsDropDown(background);
                popWindow.setOnFinishInput(new OnPasswordInputFinish() {
                    @Override
             public void inputFinish() {            
                 //输入六位密码后的回调           
                 Toast.makeText(context,popWindow.getStrPassword(), 1).show();
                //启动动画
                popWindow.StartAnima();
                    }
                });
            }
        });
  • 主要的功能逻辑是写在popwindow里面,介绍大家看看注释就好

    public PayPopWindow(final Context context, View backgroundView) {
        this.context=context;
        this.backgroundView=backgroundView;
        View view=LayoutInflater.from(context)
        .inflate(R.layout.layout_popup_bottom, null);
        //设置屏幕
        popupWindow=new PopupWindow
        (view, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        //设置popwindow弹出消失的动画效果
        popupWindow.setAnimationStyle(R.style.popWindow_anim_style);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        popupWindow.setOnDismissListener(this);// 当popWindow消失时的监听
        valueList = new ArrayList<Map<String, String>>();
        tvList = new TextView[6];
        //获取1.2.3.4.5....的初始化
        setView();

        imgCancel = (ImageView) view.findViewById(R.id.img_cance);
        tvForget = (TextView) view.findViewById(R.id.tv_forgetPwd);

        line=(LinearLayout)view.findViewById(R.id.pay_lin);
        pic=(ImageView)view.findViewById(R.id.pay_status);
        //显示的初始化
        tvList[0] = (TextView) view.findViewById(R.id.tv_pass1);
        tvList[1] = (TextView) view.findViewById(R.id.tv_pass2);
        tvList[2] = (TextView) view.findViewById(R.id.tv_pass3);
        tvList[3] = (TextView) view.findViewById(R.id.tv_pass4);
        tvList[4] = (TextView) view.findViewById(R.id.tv_pass5);
        tvList[5] = (TextView) view.findViewById(R.id.tv_pass6);

        gridView = (GridView) view.findViewById(R.id.gv_keybord);
        adapter=new PayViewAdp(context,valueList);
        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, 
            int position, long id) {
                if (position < 11 && position != 9) {    //点击0~9按钮
                    if (currentIndex >= -1 && currentIndex < 5) {      
                    //判断输入位置————要小心数组越界     
                    tvList[++currentIndex].setText(                                                      
                    valueList.get(position).get("name"));
                    }
                } else {
                    if (position == 11) {      
                    //点击退格键除完毕————要小心数组越界
                        if (currentIndex - 1 >= -1) { 
                             //判断是否点击的删
                            tvList[currentIndex--].setText("");
                        }
                    }
                }
            }
        });
        imgCancel.setOnClickListener(this);
        tvForget.setOnClickListener(this);
    }

    public interface OnItemClickListener{
        /** 设置点击确认按钮时监听接口 */
        public void onClickOKPop();
    }

    /**设置监听*/
    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener=listener;
    }


    //当popWindow消失时响应
    @Override
    public void onDismiss() {
        setBackgroundBlack(backgroundView, 1);
        popupWindow.dismiss();
    }


    /**弹窗显示的位置*/  
    public void showAsDropDown(View position){
        popupWindow.showAtLocation(position, Gravity.BOTTOM, 0,  0);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.update();
        setBackgroundBlack(backgroundView, 0);
    }


    /** 控制背景变暗 0变暗 1变亮 */
    private void setBackgroundBlack(View view, int what) {
        switch (what) {
        case 0:
            view.setVisibility(View.VISIBLE);
            break;
        case 1:
            view.setVisibility(View.GONE);
            break;
        }
    }

    public void StartAnima(){
        line.setVisibility(View.VISIBLE);
        gridView.setVisibility(View.GONE);
        // 播放逐帧动画
        animationDrawable = (AnimationDrawable) pic.getDrawable();
        animationDrawable.start();

        int duration = 0; 
        for(int i=0;i<animationDrawable.getNumberOfFrames();i++){ 
            duration += animationDrawable.getDuration(i); 
        } 
        Handler handler = new Handler(); 
        handler.postDelayed(new Runnable() { 
            public void run() { 
                //此处调用第二个动画播放方法   
                onDismiss();
//              ((Activity)context).finish();
            } 
        }, duration); 
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.img_cance:
            onDismiss();
            break;
        case R.id.tv_forgetPwd:
            Toast.makeText(context, "前往忘记密码界面", 1).show();

            break;
        default:
            break;
        }
    }

    //设置监听方法,在第6位输入完成后触发
    public void setOnFinishInput(final OnPasswordInputFinish pass) {
        tvList[5].addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, 
            int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int
             before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() == 1) {
                    strPassword = "";     
                    //每次触发都要先将strPassword置空
                    //再重新获取,避免由于输入删除再输入造成混乱
                    for (int i = 0; i < 6; i++) {
                        strPassword += tvList[i].getText().toString().trim();
                    }
                    //接口中要实现的方法,完成密码输入完成后的响应逻辑
                    pass.inputFinish();    
                }
            }
        });
    }

    /* 获取输入的密码 */
    public String getStrPassword() {
        return strPassword;
    }


    private void setView() {
        /* 初始化按钮上应该显示的数字 */
        for (int i = 1; i < 13; i++) {
            Map<String, String> map = new HashMap<String, String>();
            if (i < 10) {
                map.put("name", String.valueOf(i));
            } else if (i == 10) {
                map.put("name", "");
            } else if (i == 11) {
                map.put("name", String.valueOf(0));
            } else if (i == 12) {
                map.put("name", "<");
            }
            valueList.add(map);
        }
    }
  • 这个功能只要处理好了逻辑功能,其实做起来也挺简单的;然后顺便说一下为什么抛弃Diaolog来做这个功能的原因;
  • 1.首先Diaolog无法自主充满屏幕,设置了match也不行;
  • 2.外部点击失效,这个原因怀疑与设置了某个属性有关;
  • 3.扩展性没有popwindow高;
    下面附上下载链接:
    http://download.csdn.net/detail/qq_24535769/9601046
    希望大家喜欢,可以关注一下我的csdn噢!谢谢
    http://my.csdn.net/qq_24535769
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值