Android(安卓)Edittext 手机号码格式输入设置
16lz
2021-01-26
一. 效果图
根据需求要实现的是输入手机号码格式的数字文本,并且输入时能根据手机号码自动分隔成美观的带空格的格式,点击完成后能对手机号码验证是否是手机号码.
二. 代码实现
我使用的是动态添加代码的方式,完整代码如下.
final EditText phonEeditText=new EditText(this); phonEeditText.setId(Util.generateViewId()); phonEeditText.setTextColor(Color.WHITE); phonEeditText.setTextSize(Axis.scaleTextSize(60)); UIHelper.setHint(phonEeditText,getResources().getString(R.string.input_phone_text),Axis.scaleX(20));//设置hint提示文字的大小,后面有此方法代码 phonEeditText.setHintTextColor(ContextCompat.getColor(this,R.color.white_99_color));//设置hint文字颜色 phonEeditText.setBackground(initStateListDrawable());//设置底部下划线得到焦点时高亮 phonEeditText.setBackgroundColor(Color.TRANSPARENT); phonEeditText.setSingleLine(); phonEeditText.setInputType(EditorInfo.TYPE_CLASS_PHONE);//设置输入格式:只能输入数字 phonEeditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(13)});//设置限制长度,多了输入不了 phonEeditText.setImeOptions(EditorInfo.IME_ACTION_DONE); //关键部分:自动分隔手机号码通过addTextChangedListener()实现 phonEeditText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence charSequence, int start, int before, int count) { if (charSequence == null || charSequence.length() == 0) return; StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < charSequence.length(); i++) { if (i != 3 && i != 8 && charSequence.charAt(i) == ' ') { continue; } else { stringBuilder.append(charSequence.charAt(i)); if ((stringBuilder.length() == 4 || stringBuilder.length() == 9) && stringBuilder.charAt(stringBuilder.length() - 1) != ' ') { stringBuilder.insert(stringBuilder.length() - 1, ' '); } } } if (!stringBuilder.toString().equals(charSequence.toString())) { int index = start + 1; if (stringBuilder.charAt(start) == ' ') { if (before == 0) { index++; } else { index--; } } else { if (before == 1) { index--; } } editText.setText(stringBuilder.toString()); editText.setSelection(index); } } @Override public void afterTextChanged(Editable s) { } }); //设置光标颜色, R.drawable.cursor里自己定义颜色 try { Field f = TextView.class.getDeclaredField("mCursorDrawableRes"); f.setAccessible(true); f.set(phonEeditText, R.drawable.cursor); } catch (Exception e) { e.printStackTrace(); }
UIHelper.setHint( ) 方法:
/** * 设置Editetxt Hint 文字大小 * @param editText * @param hint * @param fontSize */ public static void setHint(EditText editText, String hint, int fontSize) { SpannableString ss = new SpannableString(hint); AbsoluteSizeSpan ass = new AbsoluteSizeSpan(fontSize, true); ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); editText.setHint(new SpannedString(ss)); // 一定要进行转换,否则属性会消失 }
R.drawable.cursor:设置光标颜色
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="#FFFFFF" />//颜色 <size android:width="1dp" />//光标宽度shape>
最后只要在父 View addView() 就能用了
RelativeLayout.LayoutParams phonEeditText_lp=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,height);parentView.addView(phonEeditText,phonEeditText_lp);
手机号码格式验证.
button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { final String phone = phonEeditText.getText().toString().trim().replace(" ", "");//去除分隔手机号中间的空格 if (phone.equals("")) {//为空 ... return; } else if (!AppUtil.isChinaPhoneLegal(phone)) {//不符合手机号码格式 ... return; } ...//服务器验证手机号码和密码是否正确、跳转 } });
验证手机格式是否正确 AppUtil.isChinaPhoneLegal( ) 方法:
/** * 是否大陆手机 * @param str * @return */ public static boolean isChinaPhoneLegal(String str) { String regExp = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$"; Pattern p = Pattern.compile(regExp); Matcher m = p.matcher(str); return m.matches(); }
更多相关文章
- Android(安卓)开发学习手记(三):关于PullToRefresh的setOnTouchList
- Android(安卓)O 8.0及其以上系统的通知(Notification)、安装apk
- android ui篇 自己写界面
- Android(安卓)Camera 预览拉伸
- Android(安卓)TextView的设置
- Android(安卓)Stutio设置注释模板
- 怎样动态更换APP图标
- android shape corners 设置无效的解决
- Android(安卓)UI设计总结