android– 如何更改EditText上的图标?

android– 如何更改EditText上的图标?,第1张

概述我使EditText接收用户的密码.所以我将输入类型更改为密码.<EditTextandroid:id="@+id/edittext_Password"android:layout_width="wrap_content"android:layout_height="wrap_content"andr

我使EditText接收用户的密码.

所以我将输入类型更改为密码.

<EditText                androID:ID="@+ID/edittext_Password"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:backgroundTint="@color/colorPrimary"                androID:inputType="textPassword"                androID:layout_weight="1"                />

当我在EditText中添加单词时,它会显示一个黑点.
我想将点更改为可绘制资源.

解决方法:

您可以按照此代码为您的要求编写代码.

答案来自this tutorial,它涵盖了用户的行为:

>进入登录界面,键盘将自动打开.
>尝试在其中输入值,然后文本框背景更改为具有星空背景的文本框.
>尝试使用键盘上的后退键取消/删除输入值,然后文本框背景将更改为没有星形背景的文本框.

首先,你必须创建两个drawable:

然后,根据这种方法,您必须在EditText上实现addTextChangedListener方法.之后,作为参数,您将创建TextWatcher类的新实例,并实现其方法:

etxtPin1.addTextChangedListener(new TextWatcher() {    @OverrIDe    public voID onTextChanged(CharSequence s, int start, int before, int count) {       // Todo auto-generated method stub    }    @OverrIDe    public voID beforeTextChanged(CharSequence s, int start, int count,                    int after) {                // Todo auto-generated method stub    }    @OverrIDe    public voID afterTextChanged(Editable s) {          if(etxtPin1.getText().toString().trim().length()==1){          etxtPin1.clearFocus();          etxtPin2.requestFocus();          etxtPin1.setBackgroundResource(R.drawable.pin_txt_bg_star);          }       }    });

然后,您必须实现setonKeyListener及其方法onKey:

this.etxtPin1.setonKeyListener(new VIEw.OnKeyListener() {      public boolean onKey(VIEw paramVIEw, int paramInt, KeyEvent paramKeyEvent) {           if ((paramKeyEvent.getAction() == KeyEvent.ACTION_DOWN)&&(paramInt == 67) && (LoginActivity.this.etxtPin2.getText().length() == 0)) {               etxtPin1.requestFocus();               etxtPin1.setBackgroundResource(R.drawable.pin_txt_bg);               etxtPin1.setText("");           }           return false;       }    });

另一种方法:创建自己的类,扩展PasswordTransformationMethod.

public class MyPasswordtransformationMethod extends PasswordtransformationMethod {    @OverrIDe    public CharSequence gettransformation(CharSequence source, VIEw vIEw) {        return new PasswordCharSequence(source);    }    private class PasswordCharSequence implements CharSequence {        private CharSequence mSource;        public PasswordCharSequence(CharSequence source) {            mSource = source; // Store char sequence        }        public char charat(int index) {            return '*'; // This is the important part        }        public int length() {            return mSource.length(); // Return default        }        public CharSequence subSequence(int start, int end) {            return mSource.subSequence(start, end); // Return default        }    }};

参考:In android how to show asterisk (*) in place of dots in EditText having inputtype as textPassword?

Reference

总结

以上是内存溢出为你收集整理的android – 如何更改EditText上的图标?全部内容,希望文章能够帮你解决android – 如何更改EditText上的图标?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1118552.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存