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
|
/*********************************************输入框关联*********************************************************/
et02.setFilters(filter);
/*********************************************调用*********************************************************/
private InputFilter[] filter =
new
InputFilter[] {
new
InputFilter() {
/**
* @param source 输入的文字
* @param start 输入-0,删除-0
* @param end 输入-文字的长度,删除-0
* @param dest 原先显示的内容
* @param dstart 输入-原光标位置,删除-光标删除结束位置
* @param dend 输入-原光标位置,删除-光标删除开始位置
* @return
*/
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
System.out.println(
"source =="
+ source+
" start====="
+start+
" end======"
+end+
" dest===="
+dest+
" dstart==="
+dstart+
" dend=="
+dend);
return
null
;
}
}
};
/*********************************************输入*********************************************************/
09-09 09:30:18.977 10843-10843/zp.example.zptext I/System.out: source ==我 start=====0 end======1 dest==== dstart===0 dend==0
09-09 09:30:21.873 10843-10843/zp.example.zptext I/System.out: source ==喜欢 start=====0 end======2 dest====我 dstart===1 dend==1
09-09 09:30:26.817 10843-10843/zp.example.zptext I/System.out: source ==湖畔 start=====0 end======2 dest====我喜欢 dstart===3 dend==3
09-09 09:31:10.265 10843-10843/zp.example.zptext I/System.out: source ==的 start=====0 end======1 dest====我喜欢湖畔 dstart===5 dend==5
/*********************************************删除*********************************************************/
09-09 09:33:08.089 10843-10843/zp.example.zptext I/System.out: source == start=====0 end======0 dest====我喜欢湖畔的 dstart===5 dend==6
09-09 09:33:22.341 10843-10843/zp.example.zptext I/System.out: source == start=====0 end======0 dest====我喜欢湖畔 dstart===4 dend==5
09-09 09:33:27.733 10843-10843/zp.example.zptext I/System.out: source == start=====0 end======0 dest====我喜欢湖 dstart===3 dend==4
09-09 09:33:30.269 10843-10843/zp.example.zptext I/System.out: source == start=====0 end======0 dest====我喜欢 dstart===2 dend==3
09-09 09:33:31.485 10843-10843/zp.example.zptext I/System.out: source == start=====0 end======0 dest====我喜 dstart===1 dend==2
09-09 09:33:32.777 10843-10843/zp.example.zptext I/System.out: source == start=====0 end======0 dest====我 dstart===0 dend==1
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
private InputFilter[] filter =
new
InputFilter[] {
new
InputFilter() {
/**
* @param source 输入的文字
* @param start 输入-0,删除-0
* @param end 输入-文字的长度,删除-0
* @param dest 原先显示的内容
* @param dstart 输入-原光标位置,删除-光标删除结束位置
* @param dend 输入-原光标位置,删除-光标删除开始位置
* @return
*/
// 不让输入框输入内容
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
return
null
;
}
},
/**这里限制输入的长度为5个字母*/
new
InputFilter.LengthFilter(5),
/**输入小写转换成大写*/
new
InputFilter.AllCaps()
};
|
1
2
3
4
5
6
7
8
9
|
// 只要你输入内容都会替换成“I LOVE YOU”,删除 - 正常删除
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if
(end > 0){
return
"I LOVE YOU"
;
}
else
{
return
null
;
}
}
|
1
2
3
4
5
6
|
// 控制不让输入空格,不让输入数字大于13位(解决手机号输入问题)
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if
(source.equals(
" "
) || source.toString().contentEquals(
"\n"
) || dstart == 13)
return
""
;
else
return
null
;
}
|
1
2
3
4
|
// 不让输入框输入内容(禁止输入)
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
return
dest.subSequence(dstart,dend);
|
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
|
/*****************************配置************************************/
private final String str =
"Hello,Android!"
;
et03.setFilters(
new
InputFilter[]{
new
MyInputFilter(str) });
/*****************************代码实现************************************/
public class MyInputFilter implements InputFilter {
String str =
null
;
public MyInputFilter(String str) {
this
.str = str;
}
@Override
public CharSequence filter(CharSequence src, int start, int end, Spanned dest, int dstart, int dend) {
String ch =
null
;
if
(dest.length() < str.length()) {
ch = str.substring(dstart + start, dstart + end);
}
else
{
return
dest.subSequence(dstart, dend);
}
if
(ch.equals(src)) {
Toast.makeText(TextWatchActivity.
this
,
"match"
, Toast.LENGTH_SHORT).show();
return
dest.subSequence(dstart, dend) + src.toString();
}
else
{
Toast.makeText(TextWatchActivity.
this
,
"mismatch"
, Toast.LENGTH_SHORT).show();
return
dest.subSequence(dstart, dend) +
"×"
;
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/**只允许输入某些字符*/
et03.setKeyListener(
new
NumberKeyListener() {
@Override
public int getInputType() {
return
InputType.TYPE_MASK_VARIATION;
}
/**这里实现字符串过滤,把你允许输入的字母添加到下面的数组即可!*/
@Override
protected char[] getAcceptedChars() {
return
new
char[]{
'0'
,
'1'
,
'2'
,
'3'
};
}
});
|
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
|
/*******************************配置************************************/
et03.setFilters(
new
InputFilter[]{lengthfilter2});
/*******************************实现************************************/
private DecimalFormat myformat =
new
DecimalFormat(
"###,###"
);
//金额的格式
private InputFilter lengthfilter2 =
new
InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
// 删除等特殊字符,直接返回
// "".equals(source.toString():不添加会出现删除不了内容的情况
if
(source.toString() ==
null
|| source.toString().contains(
"."
) ||
""
.equals(source.toString())) {
return
null
;
}
String dValue = dest.toString();
if
(dValue !=
null
&& dValue.contains(
","
)) {
dValue = dValue.replace(
","
,
""
);
}
if
(dValue !=
null
&& dValue.length() > 2 && !dValue.contains(
","
)) {
String newValue = dValue + source;
String amountStr = myformat.format(Double.parseDouble(newValue));
et03.setText(
""
);
et03.setText(amountStr);
et03.setSelection(amountStr.length());
return
amountStr;
}
else
{
return
null
;
}
}
};
|