EditText 设置
基本设置
IME Options
EditText 设置输入类型为数字
1
2
3
| android:inputType="number"
setInputType(EditorInfo.TYPE_CLASS_NUMBER);
|
EditText 设置不可编辑
1
2
| android:inputType="none"
setInputType(EditorInfo.TYPE_NULL);
|
EditText 设置可自动换行
1
| android:inputType="textMultiLine"
|
光标
textCursorDrawable 光标 Cursor 颜色及粗细
1
2
3
4
5
6
7
8
9
| <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:width="1.5dp" />
<solid android:color="@color/white" />
<padding
android:top="-5dp"
android:bottom="-5dp" />
</shape>
|
- 颜色 solid
- 粗细 size
- 高度 padding
正数让光标向上多延伸 5dp 的高度,负数让光标在下面缩短 5dp 的高度
使用
1
2
3
4
5
6
7
8
9
10
11
| <EditText
android:id="@+id/login_username_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLength="25"
android:paddingTop="@dimen/qb_px_4"
android:paddingBottom="@dimen/qb_px_4"
android:singleLine="true"
android:textCursorDrawable="@drawable/shape_login_color_cursor"
android:textSize="16sp" />
|
EditText 设置未进行输入时候光标不闪烁
1
| android:cursorVisible="false"
|
代码设置光标
1
2
3
4
5
6
7
8
9
10
11
12
13
| // 修改光标
fun EditText.textCursor(@DrawableRes textCursorDrawable: Int) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
setTextCursorDrawable(textCursorDrawable)
} else {
try { // 修改光标的颜色(反射)
val mCursorDrawableResField = this.javaClass.getDeclaredField("mCursorDrawableRes");
mCursorDrawableResField.isAccessible = true
mCursorDrawableResField.set(this, textCursorDrawable)
} catch (ignored: Exception) {
}
}
}
|
背景 background
1
2
3
4
5
| <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="@dimen/qb_px_4" />
<solid android:color="@color/black_30_percent_transparent" />
</shape>
|
限制 EditText 最大输入字符数
在 xml 文件中设置文本编辑框属性作字符数限制
android:maxLength="10"
即限制最大输入字符个数为 10
在代码中使用 InputFilter 进行过滤
[//editText.setFilters](//editText.setFilters)(new InputFilter[]{new InputFilter.LengthFilter(20)});
即限定最大输入字符数为 20
如果输入框中有自定义的 emotion,imagespan。则不行。
利用 TextWatcher 进行监听
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
| package cie.textEdit;
import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.widget.EditText;
/*
* 监听输入内容是否超出最大长度,并设置光标位置
* */
public class MaxLengthWatcher implements TextWatcher {
private int maxLen = 0;
private EditText editText = null;
public MaxLengthWatcher(int maxLen, EditText editText) {
this.maxLen = maxLen;
this.editText = editText;
}
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
Editable editable = editText.getText();
int len = editable.length();
if(len > maxLen)
{
int selEndIndex = Selection.getSelectionEnd(editable);
String str = editable.toString();
//截取新字符串
String newStr = str.substring(0,maxLen);
editText.setText(newStr);
editable = editText.getText();
//新字符串的长度
int newLen = editable.length();
//旧光标位置超过字符串长度
if(selEndIndex > newLen)
{
selEndIndex = editable.length();
}
//设置新光标所在的位置
Selection.setSelection(editable, selEndIndex);
}
}
}
|