Android 仿支付宝密码输入页面

简单页面,不一一介绍,直接上源码
`
package cn.npe1348.zfbpay.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import cn.npe1348.zfbpay.R;

public class PasswordView extends RelativeLayout {
private Context context;
// 输入的密码
private String strPassword;
// 就6个输入框不会变了,用数组内存申请固定空间,比List省空间
private TextView[]textViewList;
// 用GrideView布局键盘,其实并不是真正的键盘,只是模拟键盘的功能
private GridView gridView;
// 要用Adapter中适配,用数组不能往adapter中填充
private ArrayList> valueList;
//取消按钮
private ImageView tvCancel;
//忘记密码按钮
private TextView tvForget;
// 用于记录当前输入密码格位置
private int currentIndex = -1;

//自定义接口
public interface OnPasswordInputFinish {
    //添加密码输入完成的接口
    void inputFinish();
    //取消支付接口
    void outfo();
    //忘记密码接口
    void forgetPwd();
}

public PasswordView(Context context, AttributeSet attrs) {
    this(context, attrs,0);
}

public PasswordView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.context = context;
    //view布局
    View view = View.inflate(context, R.layout.layout_popup_bottom, null);
    valueList = new ArrayList>();
    textViewList = new TextView[6];
    //初始化控件
    tvCancel=(ImageView) view.findViewById(R.id.tvCancel);
    tvForget = (TextView) view.findViewById(R.id.tv_forgetPwd);
    textViewList[0] = (TextView) view.findViewById(R.id.tv_pass1);
    textViewList[1] = (TextView) view.findViewById(R.id.tv_pass2);
    textViewList[2] = (TextView) view.findViewById(R.id.tv_pass3);
    textViewList[3] = (TextView) view.findViewById(R.id.tv_pass4);
    textViewList[4] = (TextView) view.findViewById(R.id.tv_pass5);
    textViewList[5] = (TextView) view.findViewById(R.id.tv_pass6);
    //初始化键盘
    gridView = (GridView) view.findViewById(R.id.gv_keybord);
    //设置键盘显示按钮到集合
    setView();
    // 必须要,不然不显示控件
    addView(view);
}

//设置按钮显示内容
private void setView() {
    // 初始化按钮上应该显示的数字
    for (int i = 1; i < 13; i++) {
        Map map = new HashMap();
        if (i < 10) {
            map.put("name", String.valueOf(i));
        } else if (i == 10) {
            map.put("name", "X");
        } else if (i == 12) {
            map.put("name", "<");
        } else if (i == 11) {
            map.put("name", String.valueOf(0));
        }
        valueList.add(map);
    }
    //为键盘gridview设置适配器
    gridView.setAdapter(adapter);
    //为键盘按键添加点击事件
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView parent, View view,
                                int position, long id) {
            // 点击0~9按钮
            if (position < 11 && position != 9) {
                // 判断输入位置————要小心数组越界
                if (currentIndex >= -1 && currentIndex < 5) {
                    textViewList[++currentIndex].setText(valueList.get(position)
                            .get("name"));
                }
            } else {
                // 点击退格键
                if (position == 11) {
                    // 判断是否删除完毕————要小心数组越界
                    if (currentIndex - 1 >= -1) {
                        textViewList[currentIndex--].setText("");
                    }
                }else if(position == 9){
                    currentIndex = -1;
                    for(int i = 0,length=textViewList.length;i

}

`

你可能感兴趣的:(android)