Android自定义输入框

项目要求能够输入四行四列并包含一张图片的输入框,形成的效果类似于单个输入框的那种输入手感,之前有个版本做的三行,用的三个Edittext控制联动的,联动写起来比较麻烦,要考虑的情形有点多,所以这次使用了XRecyclerView来写布局。

 代码如下:

package com.zrodo.app.tzxmdb.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.GridLayoutManager;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zrodo.app.tzxmdb.R;
import com.zrodo.app.tzxmdb.base.BaseAdapter;
import com.zrodo.app.tzxmdb.base.BaseViewHolder;
import com.zrodo.app.tzxmdb.bean.InputBean;
import com.zrodo.app.tzxmdb.utils.glide.ImageUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by YJY on 2022/8/5 10:37.
 * 文件说明:
 */
public class InputListView extends XRecyclerView {

    private InputAdapter adapter;
    private Bitmap mBitmap;
    private List<InputBean> list = new ArrayList<>();
    private List<TextWatcher> watcher = new ArrayList<>();
    private OnContentVerListener mListener;

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

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

    public InputListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        GridLayoutManager manager = new GridLayoutManager(context, 4);
        setLayoutManager(manager);

        setPullRefreshEnabled(false);
        setLoadingMoreEnabled(false);
        adapter = new InputAdapter(context, null);
        setAdapter(adapter);
    }

    public void setContent(String content, Bitmap bitmap) {
        this.mBitmap = bitmap;
        list.clear();
        if (!TextUtils.isEmpty(content)) {
            for (int i = 0; i < content.length(); i++) {
                InputBean inputBean = new InputBean();
                if (i < content.length()) {
                    inputBean.num = String.valueOf(content.charAt(i));
                }
                list.add(inputBean);
            }
        }
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
        if (mListener != null) {
            mListener.onChangeListen(list);
        }
    }

    public String getList() {
        StringBuffer stringBuffer = new StringBuffer();
        if (list.size() > 0) {
            for (InputBean item : list) {
                stringBuffer.append(item.num);
            }
        }
        return stringBuffer.toString().trim();
    }

    public void setListener(OnContentVerListener listener) {
        this.mListener = listener;
    }


    class InputAdapter extends BaseAdapter {
        private int lastPostion = 0;//上一次光标的位置

        public InputAdapter(Context context, List list) {
            super(context, list);
        }

        @Override
        protected int getLayoutRes(int viewType) {
            if (viewType == 0) {
                return R.layout.adapter_input_1;
            } else {
                return R.layout.adapter_input;
            }
        }

        @Override
        protected int getViewType(int position, Object o) {
            return position;
        }

        @Override
        public int getItemCount() {
            return 16;
        }

        @Override
        protected void myViewHolderBind(BaseViewHolder holder, int position, List mList, Object o, int adapterPosition, int itemViewType) {
            if (position == 0) {//图片
                ((ImageView) holder.getView(R.id.iv_bitmap)).setImageBitmap(mBitmap);
            } else {
                LinearLayout view = (LinearLayout) holder.getView(R.id.ll_input);
                EditText et = (EditText) holder.getView(R.id.et_code);
                view.setBackground(mContext.getDrawable(lastPostion == position ? R.drawable.shape_input_select : position > list.size() ? R.drawable.shape_input_un_empty : R.drawable.shape_input_un_));
                et.setOnFocusChangeListener(new OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (hasFocus) {
                            if (lastPostion != position) {
                                lastPostion = position;
                                notifyDataSetChanged();
                            }
                        }
                    }
                });
                et.setOnKeyListener(null);
                et.setOnKeyListener(new OnKeyListener() {
                    @Override
                    public boolean onKey(View v, int keyCode, KeyEvent event) {
                        if (keyCode == KeyEvent.KEYCODE_DEL) {
                            int selectionEnd = et.getSelectionStart();
                            if (selectionEnd == 0) {
                                if (list.size() < position) {//删除上一个
                                    if (position != 1) {
                                        lastPostion = list.size() == 0 ? 1 : list.size();
                                        notifyDataSetChanged();
                                    }
                                } else {
                                    if (position != 1) {
                                        lastPostion = position - 1;
                                        notifyDataSetChanged();
                                    }
                                }
                            }

                        }
                        return false;
                    }
                });

                if (watcher.size() > 0) {
                    for (TextWatcher item : watcher) {
                        et.removeTextChangedListener(item);
                    }
                }

                if (position > list.size()) {
                    et.setText("");
                } else {
                    InputBean inputBean = list.get(position - 1);
                    if (inputBean != null) {
                        et.setText(inputBean.num);
                    }
                }
                if (position == lastPostion) {
                    if (!et.isFocused()) {
                        et.requestFocus();
                        et.setSelection(et.getText().toString().trim().length());
                    }

                } else {
                    et.clearFocus();
                }
                TextWatcher textWatcher = 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) {
                        if (list.size() >= 15 && s.length() > 0) {//当输入内容已经满了15位
                            if (s.length() > 1) {
                                InputBean inputBean = list.get(position - 1);
                                String trim = et.getText().toString().trim().substring(0, 1);
                                et.setText(inputBean != null ? inputBean.num : trim);
                            }
                        } else {
                            if (TextUtils.isEmpty(s.toString().trim())) {//当按到了删除键
                                if (list.size() > position - 1) {
                                    list.remove(position - 1);
                                    lastPostion = position == 1 ? 1 : position - 1;
                                }
                            } else if (s.toString().length() > 1) {//当输入内容达到了2位
                                if (list.size() > position - 1) {
                                    InputBean inputBean1 = list.get(position - 1);
                                    inputBean1.num = s.toString().substring(0, 1);
                                }
                                InputBean inputBean = new InputBean();
                                inputBean.num = s.toString().substring(1);
                                list.add(position, inputBean);


                                lastPostion = position + 1;
                            } else {//当只输入了一位
                                InputBean inputBean = new InputBean();
                                inputBean.num = s.toString();
                                if (position > list.size()) {
                                    list.add(inputBean);
                                    lastPostion = list.size() + 1;
                                } else {
                                    list.add(position - 1, inputBean);
                                    lastPostion = position + 1;
                                }
                            }
                            if (mListener != null) {
                                mListener.onChangeListen(list);
                            }
                            notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                };
                watcher.add(textWatcher);
                et.addTextChangedListener(textWatcher);


            }
        }
    }

    public interface OnContentVerListener {
        void onChangeListen(List<InputBean> b);
    }

}

 布局adapter_input_1

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/iv_bitmap"
    android:layout_width="match_parent"
    android:scaleType="fitXY"
    android:layout_height="@dimen/dp_50"
    android:layout_margin="@dimen/dp_5">

</ImageView>

  布局adapter_input

<?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="@dimen/dp_50"
    android:id="@+id/ll_input"
    android:layout_margin="@dimen/dp_5"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et_code"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@null"
        android:digits="0123456789"
        android:gravity="center"
        android:hint=""
        android:maxEms="2"
        android:inputType="number"
        android:maxLength="2"
        android:singleLine="true"
        android:textColor="@color/color_333333"
        android:textColorHint="@color/color_b6bccc"
        android:textSize="@dimen/sp_16" />

</LinearLayout>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值