如何使用自定义图像编辑文本密码,如轴银行应

时间:2023-03-29
本文介绍了如何使用自定义图像编辑文本密码,如轴银行应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

看图:

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

解决方案

答案来自.

公共类 MyPasswordTransformationMethod 扩展 PasswordTransformationMethod {@覆盖public CharSequence getTransformation(CharSequence source, View view) {return new PasswordCharSequence(source);}私有类 PasswordCharSequence 实现 CharSequence {私人 CharSequence mSource;公共密码字符序列(字符序列源){mSource = 来源;//存储字符序列}公共 char charAt(int index) {返回 '​​';//这是重要的部分}公共整数长度(){返回 mSource.length();//返回默认值}公共 CharSequence subSequence(int start, int end) {返回 mSource.subSequence(开始,结束);//返回默认值}}};

参考:在android中如何在输入类型为textPassword的EditText中显示星号(*)代替点?

How to use custom image instead of '*' in edittext password field?

see image:

Any answer or hint will be greatly appreciated.

解决方案

The answer comes from this tutorial and it covers a behaviour when a user:

  • enters into the login screen, keyboard will open automatically.

  • tries to enter value in it then textbox background changes to textbox with star background.

  • tries to cancel/delete the input value by using back key on keyboard then textbox background will change to textbox without star background.

First of all you have to create two drawables:

Then, according to this approach, you have to implement addTextChangedListener method on your EditText. After that, as a parameter, you create a new instance of a TextWatcher class and you implement its methods:

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

          }
       }
    });

Then, you have to implement setOnKeyListener and its method 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;
       }
    });


Another approach: create you own class which extends 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
        }
    }
};

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

这篇关于如何使用自定义图像编辑文本密码,如轴银行应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:EditText 在动画后卡住并在滚动时重新活着......? 下一篇:Android,如何停止出现在 EditText 下方的指针

相关文章

最新文章