Search in sources :

Example 11 with InputFilter

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);
    }
}
Also used : InputFilter(android.text.InputFilter) AccessibilityNodeInfo(android.view.accessibility.AccessibilityNodeInfo) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 12 with InputFilter

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;
        }
    } });
}
Also used : InputFilter(android.text.InputFilter) Spanned(android.text.Spanned) SuppressLint(android.annotation.SuppressLint)

Example 13 with InputFilter

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);
}
Also used : Utf8ByteLengthFilter(com.android.settings.bluetooth.Utf8ByteLengthFilter) InputFilter(android.text.InputFilter) SpannableStringBuilder(android.text.SpannableStringBuilder) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 14 with InputFilter

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 });
}
Also used : InputFilter(android.text.InputFilter) Spanned(android.text.Spanned)

Example 15 with InputFilter

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;
}
Also used : InputFilter(android.text.InputFilter) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) TextView(android.widget.TextView) TextView(android.widget.TextView) View(android.view.View) Nullable(android.support.annotation.Nullable)

Aggregations

InputFilter (android.text.InputFilter)86 EditText (android.widget.EditText)31 TextView (android.widget.TextView)27 View (android.view.View)26 DialogInterface (android.content.DialogInterface)18 AlertDialog (android.app.AlertDialog)17 Paint (android.graphics.Paint)12 Editable (android.text.Editable)12 Spanned (android.text.Spanned)10 TextPaint (android.text.TextPaint)10 LinearLayout (android.widget.LinearLayout)10 TextWatcher (android.text.TextWatcher)9 Bundle (android.os.Bundle)8 SpannableStringBuilder (android.text.SpannableStringBuilder)8 ImageView (android.widget.ImageView)8 SmallTest (android.test.suitebuilder.annotation.SmallTest)7 Button (android.widget.Button)7 Context (android.content.Context)6 AlertDialog (android.support.v7.app.AlertDialog)6 Utf8ByteLengthFilter (com.android.settings.bluetooth.Utf8ByteLengthFilter)6