简体   繁体   English

使用android中的drawable图标显示和隐藏密码

[英]show and hide password using drawable icons in android

I have designed a screen with password field using EditText, in the EditText I am using drawableRightIcon which has to show the password visible when we click the drawable button and also replace that drawable icon with another icon? 我使用EditText设计了一个带密码字段的屏幕,在EditText中,我使用的是drawableRightIcon,当我们点击drawable按钮并用另一个图标替换那个drawable图标时,它必须显示密码? can anyone help please? 有人可以帮忙吗?

Execute the following statements when your image button is clicked 单击图像按钮时执行以下语句

String password= your_editText.getText().toString().trim();
your_editText.setText(password);
your_imgview.setImageResource(R.drawable.new_image)

Following is the code that I'm using currently in my apps for this purpose. 以下是我目前在我的应用程序中用于此目的的代码。 We basically put a touch listener to our EditText and identify if the click occurred on the drawable and act accordingly (also switching icons): 我们基本上为我们的EditText了一个触摸监听器,并确定是否在drawable上发生了点击并相应地采取行动(也切换图标):

getPasswordEditText().setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                final int DRAWABLE_RIGHT = 2; // index

                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (event.getRawX() >= (getPasswordEditText().getRight() - getPasswordEditText().getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                        if (passwordShown) {
                            passwordShown = false;
                            // 129 is obtained by bitwise ORing InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD
                            getPasswordEditText().setInputType(129);

                            // Need to call following as the font is changed to mono-space by default for password fields
                            getPasswordEditText().setTypeface(Typeface.SANS_SERIF);
                            getPasswordEditText().setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.locked_icon, 0);
                        } else {
                            passwordShown = true;
                            getPasswordEditText().setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

                            getPasswordEditText().setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.unlocked_icon, 0);
                        }

                        return true;
                    }
                }
                return false;
            }
        });

Use the following code to show and hide the password. 使用以下代码显示和隐藏密码。 You can use an ImageView beside the check box. 您可以在复选框旁边使用ImageView

Used a checkbox (you can use a image button also instead of it using the same concept except that you have to use an onCLickListener instead). 使用了一个复选框(您可以使用图像按钮而不是使用相同的概念,除了您必须使用onCLickListener)。

     loginShowHidePassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // EditText loses the cursor position when you change the InputType
            int curPos = mPasswordView.getSelectionStart();
            if (isChecked) {
                mPasswordView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); 
   imageview.setImageResource(R.drawable.newImage);

            } else {
                mPasswordView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
   loginShowHidePassword.setImageResource(R.drawable.newImage);

            }
            mPasswordView.setSelection(curPos);
        }
    });

You need to save a new a new image named newImage in drawable. 您需要在drawable中保存一个名为newImage的新图像。

mPasswordView is the password EditText mPasswordView是密码EditText

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM