自定义Edittext输入字符到图像?

时间:2016-03-03 04:24:07

标签: android android-edittext

我想在用户输入字符时自定义edittext,然后edittext将其更改为图像。 看起来像图像:enter image description here

注意:●图片不是符号。

4 个答案:

答案 0 :(得分:20)

您需要扩展PasswordTransformationMethod并使用setTransformationMethod的{​​{1}}方法。

EditText

并粘贴此edt.setTransformationMethod(new CustomPasswordTransformationMethod());

CustomPasswordTransformationMethod

上面的代码将写入字符,因为它最多为5个字符,之后它将在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); } } } 中打印项目符号。

摘自this帖子

<强> 更新

最后,这是你的回答:

EditText

1。在Spannable.Factory spannableFactory; int lastIndex = -1; spannableFactory = Spannable.Factory .getInstance();

中添加addTextChangedListener
EditText
  1. 将您的drawable转换为 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) { } };

    Spannable
  2. enter image description here

答案 1 :(得分:4)

假设您要将字符 b 替换为字符。然后,您可以为此添加 TextWatcher ,如下所示

 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) {

        }
    });

<强>更新

我们可以使用setCompoundDrawablesWithIntrinsicBounds放置图片,但不能将其作为字符放置在准确的位置。我们只能定义边界。

答案 2 :(得分:2)

在xml中使用edittext inputtype的属性

android:inputType="textPassword" 

答案 3 :(得分:2)

在您的代码中尝试此操作。 doc

    EditText text = (EditText) findViewById(R.id.edtTest);
    text.setCompoundDrawablesWithIntrinsicBounds(null, null,
                       getResources().getDrawable(R.drawable.myDrawable), null);