Dostosować znak wejściowy Edittext do obrazu?

Chcę dostosować edittext, gdy użytkownik wprowadzi znak, a następnie edittext zmieni go na obrazek. Wyglądaj jak obraz : Tutaj wpisz opis obrazka

Uwaga : ● jest obrazem, a nie symbolem.

Author: Ravi Rupareliya, 2016-03-03

4 answers

Należy rozszerzyć PasswordTransformationMethod i użyć setTransformationMethod metody EditText.

edt.setTransformationMethod(new CustomPasswordTransformationMethod());

I wklej to CustomPasswordTransformationMethod

class CustomPasswordTransformationMethod extends PasswordTransformationMethod {
    @Override
    public CharSequence getTransformation(CharSequence source, View view) {
        return new PasswordCharSequence(source);
    }

    private class PasswordCharSequence implements CharSequence {
        private CharSequence source;
        public PasswordCharSequence(CharSequence source) {
            this.source = source;
        }
        public char charAt(int index) {
            if(index>4) //your own condition, when you want to hide characters.
                return 0x2022; // change this to bullets you want like '*' or '.'
            return source.charAt(index);
        }
        public int length() {
            return source.length();
        }
        public CharSequence subSequence(int start, int end) {
            return source.subSequence(start, end);
        }
    }
}

Powyższy kod będzie zapisywał znak, ponieważ jest do 5 znaków, po czym wyświetli punktory w EditText.

Odniesienie zaczerpnięte z tego postu

aktualizacja

Wreszcie Twoja odpowiedź:

Spannable.Factory spannableFactory;
int lastIndex = -1;

spannableFactory = Spannable.Factory
            .getInstance();

1. dodaj addTextChangedListener do swojego EditText.

    mEditText.addTextChangedListener(watcher);

    TextWatcher watcher = new TextWatcher() {
       @Override
       public void beforeTextChanged(CharSequence s, int start, int count, int after) {

       }

       @Override
       public void onTextChanged(CharSequence s, int start, int before, int count) {
           if (start>4) {
               mEditText.removeTextChangedListener(watcher);
               mEditText.setText(getIconText(context, s, start));
               mEditText.addTextChangedListener(watcher);
               mEditText.setSelection(s.length());
           }
       }

       @Override
       public void afterTextChanged(Editable s) {

       }
    };
  1. Przekonwertuj swój drawable na Spannable

    public Spannable getIconText(Context context, CharSequence text, int index) {
       Spannable spannable = spannableFactory.newSpannable(text);
       if (index>lastIndex) {
           spannable.setSpan(new ImageSpan(context, R.drawable.bullet_point),
                 index, index + 1,
                 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
       }
       lastIndex=index;
       return spannable;
    }
    

Tutaj wpisz opis obrazka

 20
Author: Ravi Rupareliya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:17:39

Powiedzmy, że chcesz zastąpić znak b znakiem . Następnie możesz dodać Texttwatcher do tego, co wygląda tak

 myEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            StringBuilder myText = new StringBuilder(myEditText.getText().toString());
            if (myText.toString().contains("b")){ //If this contains b
                myText.setCharAt(myText.indexOf("b"),'●');
                myEditText.setText(myText.toString()); //Sets the string to EditText
                myEditText.setSelection(myText.length()); //Moves cursor to the last after replacing
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

UPDATE

Możemy użyć setCompoundDrawablesWithIntrinsicbounds do umieszczenia obrazu, ale nie możemy umieścić go jako znaku w dokładnej pozycji. możemy określić tylko granice.

 4
Author: Shree Krishna,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-03 05:17:45

Użyj właściwości EditText inputtype w swoim xml

android:inputType="textPassword" 
 2
Author: Ajinkya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-03 04:28:42

Spróbuj tego w swoim kodzie. doc

    EditText text = (EditText) findViewById(R.id.edtTest);
    text.setCompoundDrawablesWithIntrinsicBounds(null, null,
                       getResources().getDrawable(R.drawable.myDrawable), null);
 2
Author: mubeen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-03 05:02:47