Skip to content Skip to sidebar Skip to footer

Custom Textview With Just One Character That Was Entered Last From Keyboard

Okay, I want somehow do the following. In dialog I want user to be able to choose one letter (only letters). He should be able to see it after he entered it. If user is not satisfi

Solution 1:

Class members

private MyTextWatcher mTextWatcher;
private EditText mEditText;

Call setTextWatcher() in appropriate place, for example in onCreate

publicvoidsetTextWatcher()
{
    mTextWatcher = new MyTextWatcher();
    mEditText.addTextChangedListener(mTextWatcher);
}

MyTextWatcher class

publicclassMyTextWatcherimplementsTextWatcher
{

    @OverridepublicvoidafterTextChanged(Editable s)
    {

        Stringtext= mEditText.getText().toString();
        if (text.length() > 1)
        {
            mEditText.removeTextChangedListener(mTextWatcher);
            mEditText.setText(text.substring(1));
            Selection.setSelection(mEditText.getText(), 1);
            mEditText.addTextChangedListener(mTextWatcher);
        }
    }

    @OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count,
            int after)
    {

    }

    @OverridepublicvoidonTextChanged(CharSequence s, int start, int before,
            int count)
    {

    }

}

Solution 2:

You will need to attach a text changed listener to your EditText:

This is kind of a workaround:

publicclassXYZextendsActivity {

StringtoCompare="";

....

....

....

yourEditText.addTextChangedListener(newTextWatcher() {

    @OverridepublicvoidafterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }

    @OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count, int after) {
        // TODO Auto-generated method stub

    }

    @OverridepublicvoidonTextChanged(final CharSequence s, int start, int before, int count) {

                    if (s.length() > 0 && !toCompare.equals(s.toString())) {
                            toCompare = String.valueOf(s.charAt(s.length() - 1));
                            yourEditText.setText(String.valueOf(s.charAt(s.length() - 1)));
                    }

    }

});

Post a Comment for "Custom Textview With Just One Character That Was Entered Last From Keyboard"