android 自定义键盘输入密码控件

直接上代码

public class PwdInputView extends RelativeLayout {

    private Context                        mContext;
    private GridView                       mGridView;     //支付键盘布局
    private ArrayList<Map<String, String>> valueList;
    private View                  mView;
    private OnPasswordInputFinish mPass;
    private TextView mTxtPwd;
    private ImageView mImgDelete;

    public PwdInputView(Context context) {
        super(context, null);
    }

    public PwdInputView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;

        mView = View.inflate(context, R.layout.pay_view, null);
        valueList = new ArrayList<>();
        mTxtPwd = (TextView) mView.findViewById(R.id.txt_pwd);
        mImgDelete = (ImageView) mView.findViewById(R.id.img_delete);
        mImgDelete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mTxtPwd.setText("");
            }
        });
        mGridView = (GridView) mView.findViewById(R.id.gv_keybord);
        setView();
        addView(mView); //必须要,不然不显示控件
    }

    /**
     *初始化按钮上应该显示的数字
     * */
    private void setView() {
        for (int i = 1; i < 13; i++) {
            Map<String, String> map = new HashMap<>();
            if (i < 10) {
                map.put("name", String.valueOf(i));
            } else if (i == 11) {
                map.put("name", String.valueOf(0));
            } else if (i == 10) {
                map.put("name", "删除");
            } else if (i == 12) {
                map.put("name", "确认");
            }
            valueList.add(map);
        }
        mGridView.setAdapter(adapter);
    }

    /**
     * 获取输入的密码
     */
    public String getPassword() {
        return mTxtPwd.getText().toString();
    }

    /**
     * 清空输入密码框
     * */
    public void clearPassword(){
        mTxtPwd.setText("");
    }

    /**
     *  GridView的适配器
     * */
    BaseAdapter adapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return valueList.size();
        }

        @Override
        public Object getItem(int position) {
            return valueList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.item_pay_gride, null);
                holder = new ViewHolder();
                holder.btnKey = (TextView) convertView.findViewById(R.id.btn_keys);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.btnKey.setText(valueList.get(position).get("name"));

            if (position == 11 || position ==9) {
                holder.btnKey.setTextSize(15);
            }


            holder.btnKey.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (position < 9 || position ==10) {   //点击0-9按钮
                            mTxtPwd.append(valueList.get(position).get("name"));
                    } else {
                        if (position == 9) {   //点击退格键
                            String s = mTxtPwd.getText().toString();
                            if(s.length()>0){
                                mTxtPwd.setText(s.substring(0,s.length()-1));
                            }
                        }

                        if (position == 11) {//点击确认按钮
                            mPass.inputFinish();
                        }
                    }
                }
            });

            return convertView;
        }
    };


    static class ViewHolder {
        public TextView btnKey;
    }

    /**
     * <p>
     * 自定义接口,用于给密码输入完成添加回掉事件
     */
    public interface OnPasswordInputFinish {
        void inputFinish();
    }

    public void setOnFiishCommit(OnPasswordInputFinish passfinish) {
        mPass = passfinish;
    }


}

布局pay_view:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/rlayout_pwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/txt_pwd"
            android:layout_width="match_parent"
            android:layout_height="80px"
            android:inputType="numberPassword"
            android:textColor="#ffff00"
            android:gravity="center_vertical"
            android:textSize="20pt"
            android:background="@drawable/bg_pwdview"/>
        <ImageView
            android:id="@+id/img_delete"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@drawable/pic_clear"
            android:layout_alignParentRight="true"
            android:layout_marginRight="14px"
            android:layout_centerVertical="true"/>
    </RelativeLayout>

    <!-- 输入键盘 -->

    <GridView
        android:id="@+id/gv_keybord"
        android:layout_width="500px"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rlayout_pwd"
        android:layout_marginTop="40dp"
        android:horizontalSpacing="15dp"
        android:numColumns="3"
        android:verticalSpacing="15dp"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>

item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/btn_keys"
        android:layout_width="120px"
        android:layout_height="120px"
        android:gravity="center"
        android:text="0"
        android:textColor="@color/txt_color"
        android:textSize="24pt"
        android:background="@drawable/txt_bg_selector"/>
</LinearLayout>

有一些小的背景就不写了,可以自己定义,看看效果

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

袁震

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值