android - 如何使用自定义图像作为 edittext 密码,如 axis bank app

标签 android android-edittext

如何在 edittext 密码字段中使用自定义图像而不是 '*'?

看图:

custom paasword edittext

任何答案或提示将不胜感激。

最佳答案

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

  • 进入登录界面,键盘会自动打开。

  • 尝试在其中输入值,然后文本框背景变为带有星号背景的文本框。

  • 尝试使用键盘上的返回键取消/删除输入值,然后文本框背景将变为没有星号背景的文本框。

首先你必须创建两个drawables:

enter image description here

enter image description here

然后,根据这种方法,您必须在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;
       }
    });

enter image description here


另一种方法:创建您自己的扩展 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?

关于android - 如何使用自定义图像作为 edittext 密码,如 axis bank app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24797824/

相关文章:

android - 如何让布局监听器忽略其顶部的 View - android

java - 将文件从 Android 应用程序上传到 SFTP 服务器

java - 错误: “Method getText() must be called from the UI thread, currently inferred thread is worker.”

android - android中的电话号码自动格式化

android - 如何将 OnClickListener 添加到 EditText 中的 Drawable?

android - 拦截Android中的返回键

android - IBM Worklight - 项目构建问题

Android Bitmap回收问题,是否需要维护Bitmap的所有引用,才能调用回收方法?

android - 膨胀的 EditText 的内容

android - Edittext 光标在棉花糖 rtl 支持的布局中中断