长期更新…
一、EditText
1. 常用属性
<EditText android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:background = "背景" @null透明
android:hint = "提示文本"
android:textColorHint = "提示文本颜色"
android:textColorHighlight = "被选中的字体颜色"
android:textCursorDrawable = "被光标的颜色"
android:textStyle = "字体样式" bold(加粗), italic(倾斜), normal(默认)
android:typeface = "文本字体" monospace (等宽) , sans, serif, normal(默认)
android:inputType = "输入类型"
android:digits = "只接受的指定内容" 身份证:0123456789X
android:maxLength = "文本最多个数"
android:lines = "显示行数"
android:minLines = "最小行数" 固定了宽的最小值
android:maxLines = "最大行数" 超出滚动条
android:lineSpacingExtra = "行间距"
android:lineSpacingMultiplier = "行间距倍数"
android:imeOptions = "键盘右下角动作" actionDone (完成) actionSend (发送) actionSearch (搜索)
android:selectAllOnFocus="是否获得焦点全选" />
1)inputType 输入类型/键盘行为
输入类型:
textEmailAddress(带@), textUrl(带/),number(数字), phone(电话), datetime时间日期
键盘行为:
textCapSentences (大写每个新句子的第一个字母)
textCapWords (大写每个单词)
textAutoCorrect (正常文本键盘,可纠正拼写错误的字词)
textPassword (密文显示)
2)imeOptions指定键盘右下角回车键位置:
(在使用imeOptions时,如果你没有使用inputType属性,是不会有效果的)
3)设置光标
[1] 控制光标属性 android:textCursorDrawable
设置EditView的光标颜色和text color一样:android:textCursorDrawable=“@null”
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<size android:width="2dp" />
<solid android:color="@color/color1" />
</shape>
[2] 动态设置光标颜色:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[]{
ColorUtil.COLOR_CHECKED,ColorUtil.COLOR_CHECKED});
gd.setSize(UIUtil.dip2px(mContext,1.5),UIUtil.dip2px(mContext,etSearch.getTextCursorDrawable().getIntrinsicHeight()));
etSearch.setTextCursorDrawable(gd);
}
2. 监听事件
1)监听输入框内容
需求:只能输入有限位小数,且限制最大值
etNum.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String num = etNum.getText().toString().trim();
if(TextUtils.isEmpty(num)){
return;}
//删除.后面超过2位的数字(最多可输入2位小数)
if (num.contains(".")) {
if (s.length() - 1 - s.toString().indexOf(".") > 2) {
s = s.toString().subSequence(0,s.toString().indexOf(".") + 3);
etNum.setText(s);
etNum.setSelection(s.length());
}
}
//如果.在起始位置,则起始位置自动补0
if (num.substring(0).equals(".")) {
s = "0" + s;
etNum.