public final class CharInputFilter extends InputFilter
RichText.
This API can filter characters that are not in the valid character set you specified.
To specify a valid character set, you are advised to use a regular expression, for example, [\\+-\\.\\d],
for its powerful functions.
In a simplest case, if you want to explicitly state which characters are valid, for example, abcde, then
you need to set the regular expression to [abcde].
For ease of use and understanding, the following two use cases are provided:
CharInputFilter#NUMBER and CharInputFilter#ALPHABET.
The specified regular expression is used to ensure that each character in the input text, other than the entire
input text, meets the rules of the regular expression.InputFilter.Range| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static CharInputFilter |
ALPHABET |
Indicates the alphabet filter, which can filter any characters excluding
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ". |
static CharInputFilter |
NUMBER |
Indicates the number filter, which can filter any characters excluding
"+-.0123456789". |
| 构造器 | 说明 |
|---|---|
CharInputFilter(String inRegexPattern) |
Creates a character filter using a specified regular expression.
|
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
CharSequence |
filter(CharSequence source,
InputFilter.Range sourceRange,
CharSequence dest,
InputFilter.Range destRange) |
Filters the input text, which can be plain text or
RichText. |
public static final CharInputFilter ALPHABET
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".public static final CharInputFilter NUMBER
"+-.0123456789".public CharInputFilter(String inRegexPattern)
inRegexPattern - Indicates the regular expression to use.public CharSequence filter(CharSequence source, InputFilter.Range sourceRange, CharSequence dest, InputFilter.Range destRange)
RichText.
If the source is rich text, the text itself will change. In other cases, the text will not change.
Depending on the derived class of InputFilter, determine whether to pass null to dest
and destRange.
For example, pass null if the derived class is CharInputFilter or UpperCaseFilter.
For LengthFilter, do not pass null to dest and destRange, because these two
parameters restrict the source text together with sourceRange.filter 在类中 InputFiltersource - Indicates that source strings to be filtered.sourceRange - Indicates the filtering range of the source text. Only text in this range is filtered.dest - Indicates the destination text.destRange - Indicates the filtering range of the destination text.null if the input source text is null or the
specified Range is invalid.