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

记录贴
一个组合控件
按键item布局文件

<?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="match_parent"
    android:background="#fff"
    >

    <TextView
        android:id="@+id/tv_softText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="#000"
        android:textSize="30sp"
        android:text="2"
        />

</RelativeLayout>

自定义组合控件的布局文件

<?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="match_parent"
    android:orientation="vertical"
    >

    <GridView
        android:id="@+id/gv_password"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:numColumns="6"
        android:stretchMode="columnWidth"
        android:verticalSpacing="1dp"
        android:horizontalSpacing="1dp"
        android:background="#e2e2e2"
        >
    </GridView>

    <GridView
        android:id="@+id/gv_gridView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:numColumns="3"
        android:stretchMode="columnWidth"
        android:verticalSpacing="1dp"
        android:horizontalSpacing="1dp"
        android:background="#e2e2e2"
        >
    </GridView>

</LinearLayout>

控件代码

public class SoftInput extends RelativeLayout {
    private Context context;
    private String[] s=new String[]{
            "0","1","2","3","4","5","6","7","8","","9","C"
    };

    private List<String> clickResult=new ArrayList<>();

    private GridView password,gridView;
    private GideAdapter adapter;
    private GideAdapter adapter2;

    public interface OnSoftItemClickListener{
        void OnClick(String value,int position);
    }

    private OnSoftItemClickListener onSoftItemClickListener;

    public void setOnSoftItemClickListener(OnSoftItemClickListener onSoftItemClickListener){
        this.onSoftItemClickListener=onSoftItemClickListener;
    }

    public SoftInput(Context context) {
        this(context,null);
    }

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

    public SoftInput(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context=context;
        LayoutInflater.from(context).inflate(R.layout.view_soft_input, this, true);
        gridView= (GridView) findViewById(R.id.gv_gridView);
        password= (GridView) findViewById(R.id.gv_password);
        adapter=new GideAdapter(context,s);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (onSoftItemClickListener!=null){
                    onSoftItemClickListener.OnClick(adapter.getItemValue(position),position);
                }
                if (position<9||position==10){
                    setClickResult(s[position]);
                }else if (position==s.length-1){
                    removeClickResult();
                }

            }
        });
    }

    private void setClickResult(String s){
        if (clickResult!=null&&clickResult.size()<6){
            clickResult.add(s);
            setPasswordAdapter();
        }
    }

    private void removeClickResult(){
        if (clickResult!=null&&clickResult.size()>0){
            clickResult.remove(clickResult.size()-1);
            setPasswordAdapter();
        }
    }

    private void setPasswordAdapter(){
        String[] p=clickResult.toArray(new String[clickResult.size()]);
        adapter2=new GideAdapter(context,p);
        password.setAdapter(adapter2);
    }

    public List<Integer> getPassword(){
        List<Integer> pd=new ArrayList<>();
        if (clickResult!=null&&clickResult.size()>0){
            for (int i=0;i<clickResult.size();i++){
                pd.add(Integer.parseInt(clickResult.get(i)));
            }
            return pd;
        }
        return null;
    }

    class GideAdapter extends BaseAdapter{
        private Context context;
        private String[] s;

        public GideAdapter(Context context, String[] s) {
            this.context = context;
            this.s = s;
        }

        private String getItemValue(int position){
            if (s!=null){
                return s[position];
            }
            return "";
        }

        @Override
        public int getCount() {
            return s.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder;
            if (convertView==null){
                holder=new Holder();
                convertView= LayoutInflater.from(context).inflate(R.layout.item_soft_input,parent,false);
                holder.tv= (TextView) convertView.findViewById(R.id.tv_softText);
                convertView.setTag(holder);
            }else {
                holder= (Holder) convertView.getTag();
            }
            holder.tv.setText(s[position]);
            return convertView;
        }

        class Holder {
            TextView tv;
        }

    }

}

布局文件里调用

<com.example.csd.shop.view.SoftInput
        android:id="@+id/sfi_soft"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </com.example.csd.shop.view.SoftInput>

效果图
有点丑,别笑

有需要的人可以自行添加方法,ui方面自己调整,这只是简单的实现,还需要很多修改,大神勿喷!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值