TextWatcher beforeTextChanged - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for TextWatcher beforeTextChanged method.

This code example shows how to use the following methods:afterTextChanged, beforeTextChanged, onTextChanged
        final String expected1 = "(650) 123 4";
        TextWatcher textWatcher = getTextWatcher();
 
        // Append space 
        SpannableStringBuilder number = new SpannableStringBuilder(init);
        textWatcher.beforeTextChanged(number, 9, 0, 2);
        number.insert(9, " 4"); // (650) 123 4
        Selection.setSelection(number, number.length()); // make the cursor at right of 4
        textWatcher.onTextChanged(number, 9, 0, 2);
        textWatcher.afterTextChanged(number);
        assertEquals(expected1, number.toString());
        // the cursor should still at the right of '4' 
        assertEquals(expected1.length(), Selection.getSelectionEnd(number));
 
        // Clear the current string, and start formatting again. 
        int len = number.length();
        textWatcher.beforeTextChanged(number, 0, len, 0);
        number.delete(0, len);
        textWatcher.onTextChanged(number, 0, len, 0);
        textWatcher.afterTextChanged(number);

7
CodeRank
This code example shows how to use the following methods:afterTextChanged, beforeTextChanged, onTextChanged
        final String expected1 = "(650) 123 4";
        TextWatcher textWatcher = getTextWatcher();
 
        // Append space 
        SpannableStringBuilder number = new SpannableStringBuilder(init);
        textWatcher.beforeTextChanged(number, 9, 0, 2);
        number.insert(9, " 4"); // (650) 123 4
        Selection.setSelection(number, number.length()); // make the cursor at right of 4
        textWatcher.onTextChanged(number, 9, 0, 2);
        textWatcher.afterTextChanged(number);
        assertEquals(expected1, number.toString());
        // the cursor should still at the right of '4' 
        assertEquals(expected1.length(), Selection.getSelectionEnd(number));
 
        // Clear the current string, and start formatting again. 
        int len = number.length();
        textWatcher.beforeTextChanged(number, 0, len, 0);
        number.delete(0, len);
        textWatcher.onTextChanged(number, 0, len, 0);
        textWatcher.afterTextChanged(number);

7
CodeRank
This code example shows how to use the following methods:afterTextChanged, beforeTextChanged
    } 
     
    @SmallTest 
    public void testHyphenDeletion() throws Exception { 
        SpannableStringBuilder number = new SpannableStringBuilder();
        TextWatcher tw = new PhoneNumberFormattingTextWatcher();
        number.append("555-1212");
        // Move the cursor to after the hyphen 
        Selection.setSelection(number, 4);
        // Delete the hyphen 
        tw.beforeTextChanged(number, 3, 1, 0);
        number.delete(3, 4);
        tw.afterTextChanged(number);
        // Make sure that it deleted the character before the hyphen  
        assertEquals("551-212", number.toString());
         
        // Make sure it deals with left edge boundary case 
        number.insert(0, "-");
        Selection.setSelection(number, 1);
        tw.beforeTextChanged(number, 0, 1, 0);

6
CodeRank
beforeTextChanged Method Overview
This method is called to notify you that, within s, the count characters beginning at start are about to be replaced by new text with length after.
See Code Examples for other Android TextWatcher Methods: