版权声明:本文为博主原创文章,未经博主允许不得转载。
开发过与停车场有关的程序猿应该都涉及到车牌输入的开发,一些要求不高的就直接一个简单的输入框就搞定了,但是对于用户日益增加的体验度和产品经理永远满足不了的需求设定,已经容忍不了一个简单的输入框就把车牌搞定了。所以我就做了个自定义的车牌输入控件。。。
下面先说个简单点的,也是个输入框,不过自带自定义键盘。其实还做了个类似于支付宝密码的输入框+自定义popupwindow键盘的,以后再说。。
第一步:变量声明
private Activity mActivity;
private EditText mEtCarNum;
private GridView mGridView;
// 定义省份简称,
private String[] mItemContent = null;//条目内容
private List<String> mItemContentList = null;
private StringBuilder mEtContent;
private ProviceAdapter mProviceAdapter;
private LinearLayout mLinearLayout;
private static final String CLICK_PROVINCE_ABBREVIATION = "province_abbr";
private static final String CLICK_LETTER= "province_letter";
private static final String CLICK_NUMBER_AND_LETTER= "province_number_letter3";
private static final String CLICK_NUMBER_OR_LETTER= "province_number_letter4";
private String isClick = CLICK_PROVINCE_ABBREVIATION;
这个些都是要用到的,也是随手命名,不要纠结命名的规范,我做的不好。。
这里CLICK_PROVINCE_ABBREVIATION、CLICK_LETTER、CLICK_NUMBER_AND_LETTER、CLICK_NUMBER_OR_LETTER指的是4种不同的键盘内容,分别为全部是汉字,英文,数字+英文和数字+英文+汉字。
第二步:构造器
public CarNumKeyBoardUtils(Activity activity, EditText edit, GridView mGridView) {
this.mActivity = activity;
this.mEtCarNum = edit;
this.mGridView = mGridView;
initKeyBoardData();
}
//这个用于控制键盘的显示与隐藏
public CarNumKeyBoardUtils(Activity activity, EditText edit, GridView mGridView,LinearLayout mLinearLayout) {
this.mActivity = activity;
this.mEtCarNum = edit;
this.mGridView = mGridView;
this.mLinearLayout = mLinearLayout;
initKeyBoardData();
}
第三步:初始化
private void initKeyBoardData() {
String mEtStr = mEtCarNum.getText().toString();
if (!StringUtils.isEmpty(mEtStr) && mEtStr.contains("-")) {
//如果传递过来的值包含-
mEtStr = mEtStr.replace("-","");
}
mEtContent = new StringBuilder(mEtStr);
initGvData(mEtContent.toString());
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (isClick) {
case CLICK_PROVINCE_ABBREVIATION:
if (!StringUtils.isEmpty(mItemContent[position])) {
mEtContent.append(mItemContent[position]);
}
break;
case CLICK_LETTER:
mEtContent.append(mItemContent[position]);
break;
case CLICK_NUMBER_AND_LETTER:
mEtContent.append(mItemContent[position]);
break;
case CLICK_NUMBER_OR_LETTER:
int start = mEtCarNum.getSelectionStart();
if (mEtContent.length() < 7) {
mEtContent.append(mItemContent[position]);
} else if (mEtContent.length() == 7) {
String str = mEtContent.toString().substring(mEtContent.length() - 1, mEtContent.length());
if (isContain(mItemContent[position])) {
mEtContent.replace(mEtContent.length() - 1, mEtContent.length(), mItemContent[position]);
} else {
if (isContain(str)) {
mEtContent.replace(mEtContent.length() - 1, mEtContent.length(), mItemContent[position]);
} else {
mEtContent.append(mItemContent[position]);
}
}
} else if (mEtContent.length() == 8) {
mEtContent.replace(mEtContent.length() - 1, mEtContent.length(), mItemContent[position]);
}
break;
default:
break;
}
mEtCarNum.setText(mEtContent.toString());
}
});
mEtCarNum.addTextChangedListener(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 (s.toString().trim().length() > 0) {
mEtCarNum.setCursorVisible(false);
}
initGvData(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
private boolean isContain(String str){
String s = "港澳学警";
if (s.indexOf(str)>-1) {
return true;
}
return false;
}
刚初始化的时候调用initGvData()这个方法,加载键盘数据,最开始是展示全中文的键盘。
initGvData()这个是给自定义的键盘添加数据与改变数据,也是很重要的一步。
在onItemClick监听事件中,对点击事件进行了监听,将点击的内容拼接到输入框中。并且添加了addTextChangedListener事件,这样就可以实现输入一个内容后,键盘内容变更。当然,也可以直接在点击事件中处理,点击每个item后,变化键盘内容,这样会稍微麻烦点。
由于有些车牌可能是港澳学警类型的车牌,所以增加了判断。
由于有时候用户会长按输入框,然后弹出个复制粘贴的选项,觉得会影响用户体验,所以就给输入框添加了屏蔽输入框的长按复制粘贴功能。
mEtCarNum.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
}
});
mEtCarNum.setLongClickable(false);
第四步:加载键盘数据
键盘内容的加载我用的是读取资源文件下的数组。在values下新建一个arrays.xml文件,创建string-array,然后填充对应的item内容就好了。因为有些地方出现了新能源车牌,是8位,所以我写的输入框长度是8位的。用isClick这个来标识输入到了什么位置,该显示什么内容的键盘。
private void initGvData(String s){
switch (s.length()) {//传递过来的车牌号带横线
case 0:
mItemContent = mActivity.getResources().getStringArray(
R.array.province_abbr);
isClick = CLICK_PROVINCE_ABBREVIATION;
resetGvParameters();
break;
case 1:
mItemContent = mActivity.getResources().getStringArray(
R.array.province_letter);
isClick = CLICK_LETTER;
resetGvParameters();
break;
case 2:
mItemContent = mActivity.getResources().getStringArray(
R.array.province_number_letter);
isClick = CLICK_NUMBER_AND_LETTER;
resetGvParameters();
break;
case 3:
mItemContent = mActivity.getResources().getStringArray(
R.array.province_number_letter);
isClick = CLICK_NUMBER_AND_LETTER;
resetGvParameters();
break;
case 4:
mItemContent = mActivity.getResources().getStringArray(
R.array.province_number_letter);
isClick = CLICK_NUMBER_AND_LETTER;
resetGvParameters();
break;
case 5:
mItemContent = mActivity.getResources().getStringArray(
R.array.province_number_letter);
isClick = CLICK_NUMBER_AND_LETTER;
resetGvParameters();
break;
case 6:
mItemContent = mActivity.getResources().getStringArray(
R.array.province_number_2_letter);
isClick = CLICK_NUMBER_OR_LETTER;
mGridView.setNumColumns(7);
mGridView.setVerticalSpacing(ScreenUtils.px2dip(mActivity, mGridView.getHorizontalSpacing()));
break;
case 7:
mItemContent = mActivity.getResources().getStringArray(
R.array.province_number_2_letter);
isClick = CLICK_NUMBER_OR_LETTER;
mGridView.setNumColumns(7);
mGridView.setVerticalSpacing(ScreenUtils.px2dip(mActivity, mGridView.getHorizontalSpacing()));
break;
default:
break;
}
mProviceAdapter = new ProviceAdapter(mActivity, mItemContent);
mGridView.setAdapter(mProviceAdapter);
}
resetGvParameters()方法是用来重置gridview的参数,设置每个item间距的和每行显示多个item。
private void resetGvParameters() {
mGridView.setHorizontalSpacing(ScreenUtils.px2dip(mActivity, 6));
mGridView.setVerticalSpacing(ScreenUtils.px2dip(mActivity, 6));
mGridView.setNumColumns(6);
}
第五步:添加删除功能
public void deleteEtData(){
try {
if (!StringUtils.isEmpty(mEtCarNum.getText().toString())){
mEtContent.delete(mEtContent.length() - 1, mEtContent.length());
mEtCarNum.setText(mEtContent.toString());
initGvData(mEtContent.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void deleteEtAllData(){
try {
if (!StringUtils.isEmpty(mEtCarNum.getText().toString())){
mEtContent.delete(0,mEtContent.length());
mEtCarNum.setText("");
initGvData(mEtContent.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
是一个个删还是一次性删除完就看你要调那个方法了。这样基本就满足了。在使用调用这个类时,键盘默认是不显示,这个时候调用显示与隐藏的方法就可以了。对了。由于EditText会调用到系统自带的键盘,所以又写了个屏蔽系统键盘的方法。
carNumKeyBoardUtils = new CarNumKeyBoardUtils(this,mEtCarNum,mGridView,mCarNumKeyBoardLl);
carNumKeyBoardUtils.showKeyboard();
/**
* 软键盘展示
*/
public void showKeyboard() {
int visibility = mLinearLayout.getVisibility();
if (visibility == View.GONE || visibility == View.INVISIBLE) {
mLinearLayout.setVisibility(View.VISIBLE);
}
}
/**
* 软键盘隐藏
*/
public void hideKeyboard() {
int visibility = mLinearLayout.getVisibility();
if (visibility == View.VISIBLE) {
mLinearLayout.setVisibility(View.INVISIBLE);
}
}
一个带自定义键盘的车牌输入框就搞定了。原理很简单,实现出来的效果也不错,能满足需求。希望对大家有帮助。