需求自定义车牌输入框,可以动态添加输入框个数,7位正常车牌,8位新能源车牌。点击可修改输入过的车牌号码,附带点中效果。此输入框是基于一个方框样式的EditText,按照实际需求进行扩展的。
现已更新至 GitHub,可直接依赖。传送门-> LicensePlateUtil ,喜欢的给点个星,谢谢啦。。。
动态效果如下:
实现思路:
1.用一个透明的 EditText 与 8 个 TextView 重叠,并给 TextView 设置默认背景
2.监听 EditText 文本变化,获取输入内容,给 TextView 赋值并改变 TextView 背景
3.修改已输入文本,设置修改回调监听,根据对应的位置修改或删除对应的文本,同时设置选中背景
4.EditText输入监听,设置对应位置的显示内容,监听输入完成,监听删除键添加删除回调
贴代码,看逻辑处理
注:下面为最新处理逻辑,若有好的建议请在 gitHub 参与。
public class LicensePlateView extends RelativeLayout implements View.OnClickListener {
private EditText editText;
private TextView[] TextViews;
private Activity mActivity;
private View mNumView;
private View mProvinceView;
private int count = 0;
private int updateViewPosition;
private static int ITEM_VIEW_COUNT = 7;
private LayoutInflater mInflater;
private String inputContent;
private boolean isUpdateView = false;//是否更新view内容
private StringBuffer stringBuffer = new StringBuffer();
private OnFrameTouchListener mTouchListener = new OnFrameTouchListener();
private static final int[] VIEW_IDS = new int[]{
R.id.item_code_iv1, R.id.item_code_iv2, R.id.item_code_iv3,
R.id.item_code_iv4, R.id.item_code_iv5, R.id.item_code_iv6,
R.id.item_code_iv7, R.id.item_code_iv8
};
private static final int[] VIEW_PROVINCE_IDS = new int[]{
R.id.select_province_11_tv, R.id.select_province_12_tv, R.id.select_province_13_tv,
R.id.select_province_14_tv, R.id.select_province_15_tv, R.id.select_province_16_tv,
R.id.select_province_17_tv, R.id.select_province_18_tv, R.id.select_province_19_tv,
R.id.select_province_110_tv,
R.id.select_province_21_tv, R.id.select_province_22_tv, R.id.select_province_23_tv,
R.id.select_province_24_tv, R.id.select_province_25_tv, R.id.select_province_26_tv,
R.id.select_province_27_tv, R.id.select_province_28_tv, R.id.select_province_29_tv,
R.id.select_province_210_tv,
R.id.select_province_31_tv, R.id.select_province_32_tv, R.id.select_province_33_tv,
R.id.select_province_34_tv, R.id.select_province_35_tv, R.id.select_province_35_tv,
R.id.select_province_36_tv, R.id.select_province_37_tv, R.id.select_province_38_tv,
R.id.select_province_41_tv, R.id.select_province_42_tv, R.id.select_province_43_tv,
R.id.select_province_delete_tv
};
private static final int[] VIEW_NUM_IDS = new int[]{
R.id.select_num_100_tv, R.id.select_num_101_tv, R.id.select_num_102_tv,
R.id.select_num_103_tv, R.id.select_num_104_tv, R.id.select_num_105_tv,
R.id.select_num_106_tv, R.id.select_num_107_tv, R.id.select_num_108_tv,
R.id.select_num_109_tv,
R.id.select_num_200_tv, R.id.select_num_201_tv, R.id.select_num_202_tv,
R.id.select_num_203_tv, R.id.select_num_204_tv, R.id.select_num_205_tv,
R.id.select_num_206_tv, R.id.select_num_207_tv, R.id.select_num_208_tv,
R.id.select_num_209_tv,
R.id.select_num_300_tv, R.id.select_num_301_tv, R.id.select_num_302_tv,
R.id.select_num_303_tv, R.id.select_num_304_tv, R.id.select_num_305_tv,
R.id.select_num_306_tv, R.id.select_num_307_tv, R.id.select_num_308_tv,
R.id.select_num_309_tv,
R.id.select_num_400_tv, R.id.select_num_401_tv, R.id.select_num_402_tv,
R.id.select_num_403_tv, R.id.select_num_404_tv, R.id.select_num_405_tv,
R.id.select_num_406_tv,
R.id.select_num_delete_tv
};
public LicensePlateView(Context context) {
this(context, null);
}
public LicensePlateView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public LicensePlateView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mActivity = (Activity) context;
TextViews = new TextView[8];