use of android.text.InputFilter in project android_frameworks_base by AOSPA.
the class TextView method onInitializeAccessibilityNodeInfoInternal.
/** @hide */
@Override
public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfoInternal(info);
final boolean isPassword = hasPasswordTransformationMethod();
info.setPassword(isPassword);
info.setText(getTextForAccessibility());
if (mBufferType == BufferType.EDITABLE) {
info.setEditable(true);
if (isEnabled()) {
info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_SET_TEXT);
}
}
if (mEditor != null) {
info.setInputType(mEditor.mInputType);
if (mEditor.mError != null) {
info.setContentInvalid(true);
info.setError(mEditor.mError);
}
}
if (!TextUtils.isEmpty(mText)) {
info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_LINE | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PAGE);
info.addAction(AccessibilityNodeInfo.ACTION_SET_SELECTION);
}
if (isFocused()) {
if (canCopy()) {
info.addAction(AccessibilityNodeInfo.ACTION_COPY);
}
if (canPaste()) {
info.addAction(AccessibilityNodeInfo.ACTION_PASTE);
}
if (canCut()) {
info.addAction(AccessibilityNodeInfo.ACTION_CUT);
}
if (canShare()) {
info.addAction(new AccessibilityNodeInfo.AccessibilityAction(ACCESSIBILITY_ACTION_SHARE, getResources().getString(com.android.internal.R.string.share)));
}
if (canProcessText()) {
// also implies mEditor is not null.
mEditor.mProcessTextIntentActionsHandler.onInitializeAccessibilityNodeInfo(info);
}
}
// Check for known input filter types.
final int numFilters = mFilters.length;
for (int i = 0; i < numFilters; i++) {
final InputFilter filter = mFilters[i];
if (filter instanceof InputFilter.LengthFilter) {
info.setMaxTextLength(((InputFilter.LengthFilter) filter).getMax());
}
}
if (!isSingleLine()) {
info.setMultiLine(true);
}
}
use of android.text.InputFilter in project RxTools by vondear.
the class RxTool method setEdDecimal.
public static void setEdDecimal(EditText editText, int count) {
if (count < 0) {
count = 0;
}
count += 1;
editText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);
// 设置字符过滤
final int finalCount = count;
editText.setFilters(new InputFilter[] { new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (".".contentEquals(source) && dest.toString().length() == 0) {
return "0.";
}
if (dest.toString().contains(".")) {
int index = dest.toString().indexOf(".");
int mlength = dest.toString().substring(index).length();
if (mlength == finalCount) {
return "";
}
}
if (dest.toString().equals("0") && source.equals("0")) {
return "";
}
return null;
}
} });
}
use of android.text.InputFilter in project android_packages_apps_Settings by LineageOS.
the class Utf8ByteLengthFilterTest method testFilter.
@SmallTest
public void testFilter() {
// Define the variables
CharSequence source;
SpannableStringBuilder dest;
// Constructor to create a LengthFilter
InputFilter lengthFilter = new Utf8ByteLengthFilter(10);
InputFilter[] filters = { lengthFilter };
// filter() implicitly invoked. If the total length > filter length, the filter will
// cut off the source CharSequence from beginning to fit the filter length.
source = "abc";
dest = new SpannableStringBuilder("abcdefgh");
dest.setFilters(filters);
dest.insert(1, source);
String expectedString1 = "aabbcdefgh";
assertEquals(expectedString1, dest.toString());
dest.replace(5, 8, source);
String expectedString2 = "aabbcabcgh";
assertEquals(expectedString2, dest.toString());
dest.insert(2, source);
assertEquals(expectedString2, dest.toString());
dest.delete(1, 3);
String expectedString3 = "abcabcgh";
assertEquals(expectedString3, dest.toString());
dest.append("12345");
String expectedString4 = "abcabcgh12";
assertEquals(expectedString4, dest.toString());
// 2 Chinese chars == 6 bytes in UTF-8
source = "\u60a8\u597d";
dest.replace(8, 10, source);
assertEquals(expectedString3, dest.toString());
dest.replace(0, 1, source);
String expectedString5 = "\u60a8bcabcgh";
assertEquals(expectedString5, dest.toString());
dest.replace(0, 4, source);
String expectedString6 = "\u60a8\u597dbcgh";
assertEquals(expectedString6, dest.toString());
// 2 Latin-1 chars == 4 bytes in UTF-8
source = "\u00a3\u00a5";
dest.delete(2, 6);
dest.insert(0, source);
String expectedString7 = "\u00a3\u00a5\u60a8\u597d";
assertEquals(expectedString7, dest.toString());
dest.replace(2, 3, source);
String expectedString8 = "\u00a3\u00a5\u00a3\u597d";
assertEquals(expectedString8, dest.toString());
dest.replace(3, 4, source);
String expectedString9 = "\u00a3\u00a5\u00a3\u00a3\u00a5";
assertEquals(expectedString9, dest.toString());
// filter() explicitly invoked
dest = new SpannableStringBuilder("abcdefgh");
CharSequence beforeFilterSource = "TestLengthFilter";
String expectedAfterFilter = "TestLength";
CharSequence actualAfterFilter = lengthFilter.filter(beforeFilterSource, 0, beforeFilterSource.length(), dest, 0, dest.length());
assertEquals(expectedAfterFilter, actualAfterFilter);
}
use of android.text.InputFilter in project Slide by ccrama.
the class EditTextValidator method validateUsername.
/**
* Validates EditTexts intended for reddit username input. Valid characters include:
* A-Z, a-z
* 0-9
* - (hyphen)
* _ (underscore)
*
* @param editText The EditText to validate a username for
*/
public static void validateUsername(final EditText editText) {
if (editText == null)
return;
InputFilter filter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
char character = source.charAt(i);
if (!Character.isLetterOrDigit(character) && character != '_' && character != '-') {
return "";
}
}
return null;
}
};
editText.setFilters(new InputFilter[] { filter });
}
use of android.text.InputFilter in project android_packages_apps_Dialer by LineageOS.
the class MessageComposerFragment method onCreateView.
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
charLimit = getArguments().getInt(CHAR_LIMIT_KEY, NO_CHAR_LIMIT);
View view = inflater.inflate(R.layout.fragment_message_composer, container, false);
TextView urgent = (TextView) view.findViewById(R.id.message_urgent);
customMessage = (EditText) view.findViewById(R.id.custom_message);
urgent.setOnClickListener(this);
customMessage.addTextChangedListener(this);
customMessage.setOnEditorActionListener(this);
if (charLimit != NO_CHAR_LIMIT) {
TextView remainingChar = (TextView) view.findViewById(R.id.remaining_characters);
remainingChar.setText("" + charLimit);
customMessage.setFilters(new InputFilter[] { new InputFilter.LengthFilter(charLimit) });
customMessage.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
remainingChar.setText("" + (charLimit - editable.length()));
}
});
}
view.findViewById(R.id.message_chat).setOnClickListener(this);
view.findViewById(R.id.message_question).setOnClickListener(this);
return view;
}
Aggregations