将Edittext输入字符定制为图像?

12
我希望在用户输入字符后,将EditText更改为图像。我想要的效果如下图所示:enter image description here 注意: ● 是一张图片而不是符号。

当用户输入内容时,您可以将字符替换为。在用替换之前,使用EditorActionListener保存用户输入的文本。不确定图片怎么处理。 - Dhinakaran Thennarasu
请展示一下你目前所尝试过的内容。 - Mangesh
现在我还没有做任何事情。因为我对此没有经验。 - Holi Boom
4个回答

20

你需要扩展 PasswordTransformationMethod 并使用 EditTextsetTransformationMethod 方法。

edt.setTransformationMethod(new CustomPasswordTransformationMethod());

并粘贴此 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);
        }
    }
}

上述代码将原样写入字符,直到5个字符,之后它将在EditText中打印出点。

参考自这篇文章

更新

最终这就是你的答案:

Spannable.Factory spannableFactory;
int lastIndex = -1;

spannableFactory = Spannable.Factory
            .getInstance();

1. 在你的 EditText 中添加 addTextChangedListener

    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

  • 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;
    }
    

    这里输入图片描述


    1
    是的,谢谢。但我不想将它更改为项目符号或“*”,我想更改为图像。 - Holi Boom

    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来放置图片,但无法将其放置在精确位置作为字符。我们只能定义边界。


    1
    他必须用图像而不是任何字符来更改它,你有任何想法吗? - Anuj Sharma
    1
    @AnujSharma 我们可以使用 setCompoundDrawablesWithIntrinsicBounds 来实现,但无法将其放置在精确的位置上。我们只能定义边界。 - Shree Krishna

    2
    在您的XML中使用EditText的inputType属性。
    android:inputType="textPassword" 
    

    android:inputType="textPassword" 将所有字符更改为掩码,但我希望某些字符不是掩码,而其他字符则使用我的图像掩码作为掩码。 - Holi Boom
    2
    @HuoChhunleng 在这种情况下,您需要自定义 PasswordTransformationMethod,创建一个扩展 PasswordTransformationMethod 的自定义类,并实现您的逻辑。 - Ajinkya

    2

    在你的代码中尝试使用这个功能。 文档

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

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接