如何在按下按钮之前显示密码

时间:2016-08-06 20:56:15

标签: android android-layout android-edittext

我创建了一个Password输入字段和一个button,当您点击该按钮时,它会在Text Format中显示密码。

问题在于,它不会再次隐藏它,我希望在按下按钮之前显示密码,当用户取消按钮时,它应该返回到原始密码dotted模式

我编写了以下代码,它将Dotted password更改为文字,但未将text更改为dotted-password

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

            if(event.getAction() == MotionEvent.ACTION_DOWN) {
                if(event.getRawX() >= (tvEmail.getRight() - tvEmail.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                    tvPassword.setInputType(InputType.TYPE_CLASS_TEXT);
                    return true;
                }
            }else{
                tvPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD );
            }

            if(event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                if(event.getRawX() >= (tvEmail.getRight() - tvEmail.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                    tvPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD );
                    return true;
                }
            }


            return false;
        }
    });

2 个答案:

答案 0 :(得分:1)

试试这个:

passwordShown = false;

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

            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                if(event.getRawX() <= (tvEmail.getRight() - tvEmail.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                    tvPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    passwordShown = true;
                    return true; 
                } 
            } 
            else if ((event.getAction() == MotionEvent.ACTION_UP ||
                      event.getAction() == MotionEvent.ACTION_CANCEL ||
                      event.getAction() == MotionEvent.ACTION_OUTSIDE) && passwordShown) {
                tvPassword.setInputType(InputType.TYPE_CLASS_TEXT );
                passwordShown = false;
                return true; 
            } 

            return false; 
        } 
    }); 

答案 1 :(得分:1)

您可以尝试此代码

 tvEmail.setOnTouchListener(new View.OnTouchListener() { 
             @Override 
             public boolean onTouch(View v, MotionEvent event) {

                 if (event.getAction() == MotionEvent.ACTION_DOWN) 
                 {
                     tvPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                     return true; 
                 } 
                 else if (event.getAction() == MotionEvent.ACTION_UP) {
                     tvPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                     return true; 
                 } 
                 return false; 
             } 
         }); 

//考虑到tvEmail是Button或TextView,因此上面的代码可以帮助您实现所需的功能