editText的限定输入
inputType属性和digits属性
https://www.jianshu.com/p/bd4273c12e5b
1,设置editText可编辑和不可编辑状态
不可编辑状态
editText.setClicked(false);
editText.setFocusable(false);
editText.setFocusableInTouchMode(false);
可编辑状态
editText.setClicked(true);
editText.setFocusableInTouchMode(true);
editText.setFocusable(true);
editText.requestFocus();
2更改下划线样式
https://segmentfault.com/a/1190000009507919
方法1,设置一个主题风格并引用
<style name="myEditText" parent="Theme.AppCompat.Light">
//默认的颜色
<item name="colorControlNormal">@color/color_white</item>
//激活(获取焦点)的颜色
<item name="colorControlActivated">@color/color_orange</item>
</style>
android:theme="@style/myEditText"
方法2,更改默认主题的颜色设置,这个是全局变量,意思是更改后,整个应用的对应颜色都会变化
更改自己的theme设置中colorAccent属性即可
3设置密码显示和可见
https://blog.youkuaiyun.com/tianhe718/article/details/53099322
方法1
evPasswordValue.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
EditText ev=(EditText) v;
if (hasFocus) {
ev.setInputType(InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT);
} else {
ev.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD);
}
}
});
注意,设置密码不可见,需要同时设置InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD才有效
方法2
evPasswordValue.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
EditText ev=(EditText) v;
if (hasFocus) {
ev.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
} else {
ev.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
});
4 监听文字变化
(1)监听输入完成后,点击确认
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Log.i(TAG, "onEditorAction: v="+v);
Log.i(TAG, "onEditorAction: actionId="+actionId);
Log.i(TAG, "onEditorAction: event="+event);
return true;
}
});
控制台显示
查看
TextView.OnEditorActionListener()
源码参数说明
* Called when an action is being performed.
当一个动作发生时调用
*
* @param v The view that was clicked.
(点击的控件)
* @param actionId Identifier of the action. This will be either the
* identifier you supplied, or {@link EditorInfo#IME_NULL
* EditorInfo.IME_NULL} if being called due to the enter key
* being pressed.
(动作标识符)
可以自定义或者如果由于输入键被按下而被调用,返回默认的通用未指定(EditorInfo.IME_NULL)
* @param event If triggered by an enter key, this is the event;
* otherwise, this is null.
(触发事件的动作ID)
如果触发的是enter键,event就是输入事件,否则就是null
* @return Return true if you have consumed the action, else false.
如果处理该event就返回true,否则就返回false
(2)动态监听text变化
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
Log.i(TAG, "beforeTextChanged: s="+s);
Log.i(TAG, "beforeTextChanged: start="+start);
Log.i(TAG, "beforeTextChanged: count="+count);
Log.i(TAG, "beforeTextChanged: after="+after);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.i(TAG, "onTextChanged: s="+s);
Log.i(TAG, "onTextChanged: start="+start);
Log.i(TAG, "onTextChanged: before="+before);
Log.i(TAG, "onTextChanged: count="+count);
}
@Override
public void afterTextChanged(Editable s) {
Log.i(TAG, "afterTextChanged: s="+s);
}
});
控制台输出,输入ww
按照顺序执行3个函数
beforeTextChanged(),
onTextChanged()
afterTextChanged()
可以在
afterTextChanged()
中获取实时editText中的文本内容
String text=s.toString();
5.打开活动editText控件自动获取焦点,弹出软键盘
evSearch.requestFocus();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
关闭控件时取消焦点,尝试隐藏输入法
evSearch.requestFocus();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(Objects.requireNonNull(Objects.requireNonNull(getActivity()).getCurrentFocus()).getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
具体参考Android InputMethodManager输入法简介
相反,如果打开活动时,不希望editText自动获取焦点,打开软键盘
evSearch.setFocusable(false);
evSearch.setFocusableInTouchMode(false);
evSearch.clearFocus();
evSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!evSearch.hasFocus())
evSearch.setFocusable(true);
evSearch.setFocusableInTouchMode(true);
evSearch.requestFocus();
}
});
6 editText实用的配置属性
在配置editText属性时,设置
// 取消下划线
android:background="@null"
// 提示文字
android:hint="@string/length_not_exceeding_100"
// 自动换行
android:inputType="textMultiLine"
// 文字长度不超过100
android:maxLength="100"
// 限定输入字符串范围
android:digits="0123456789qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM"
7.正则表达式的使用
不用浓缩,最简单的方式
// 对一个类似于 "2018年08月25日 14时13分35秒"或者"2018-10-01"的字符串做转换"08月25日"
public String getDateFromString(String s) {
String date="";
if (s==null){
return date;
}
if (s.contains("年")){
date=s.substring(s.indexOf("年")+1,s.indexOf("日")+1);
}
if (s.contains("-")){
String regEx = "-[0-9]{2}-[0-9]{2}";
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
date = matcher.group().substring(1);
date = date.replace("-", "月")+"日";
}
}
return date;
}
// 对一个类似于 "2018年08月25日 14:13:35"的字符串做转换"14:13"
public String getTimeFromString(String s) {
String time="";
if (s==null){
return time;
}
getDateFromString(s);
if (s.contains(":")){
String regEx = "[0-9]{2}:[0-9]{2}:";
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
time = matcher.group();
time=time.substring(0,time.length()-1);
}
}
if (s.contains("时")&&s.contains("分")){
String regEx = "[0-9]{2}时[0-9]{2}分";
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
time = matcher.group();
time=time.substring(0,time.length()-1);
time=time.replace("时",":");
}
}
return time;
}
// 手机号校验
// 移动号段:139 138 137 136 135 134 147 150 151 152 157 158 159 178 182 183 184 187 188
// 联通号段:130 131 132 155 156 185 186 145 176
// 电信号段:133 153 177 173 180 181 189
// 虚拟运营商号段:170 171
public boolean checkPhoneNumber(String number){
String regEx = "1(39|38|37|36|35|34|47|50|51|52|57|58|59|78|82|83|84|87|88|"+
"30|31|32|55|56|85|86|45|76|"+
"33|53|77|73|80|81|89|"+
"70|71)[0-9]{8}";
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(number);
return !matcher.find();
}
{n,m}对前一个字符/(组合)/[范围]匹配最少n次,最多m次
abc 对abc进行匹配
abc{2} 对c匹配两次 匹配字符串abcc
(abc){2} 对abc匹配两次 匹配字符串abcabc
ab[0123456]{2} 前两位匹配ab,第三位再0-6范围内匹配两次