【问题标题】:show and hide password using drawable icons in android在android中使用可绘制图标显示和隐藏密码
【发布时间】:2016-12-24 02:46:18
【问题描述】:

我使用 EditText 设计了一个带有密码字段的屏幕,在 EditText 中我使用的是 drawableRightIcon,它必须在我们单击可绘制按钮时显示密码可见,并将该可绘制图标替换为另一个图标?有人可以帮忙吗?

【问题讨论】:

    标签: java android passwords icons drawable


    【解决方案1】:

    使用以下代码显示和隐藏密码。您可以在复选框旁边使用ImageView

    使用了一个复选框(您也可以使用图像按钮来代替它,使用相同的概念,除了您必须使用 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);
            }
        });
    

    您需要在drawable中保存一个名为newImage的新图像。

    mPasswordView是密码EditText

    【讨论】:

    • 如果你知道如何使用 if 大括号内的条件。请让我知道 onclicklistner()
    【解决方案2】:

    点击图片按钮时执行以下语句

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

    【讨论】:

      【解决方案3】:

      以下是我目前在我的应用程序中为此目的使用的代码。我们基本上为EditText 设置了一个触摸监听器,并确定点击是否发生在可绘制对象上并采取相应的行动(也切换图标):

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

      【讨论】:

      • 使用此代码时,第一次图标正在更改但密码不可见,如果我再次单击该图标然后它正在更改,第一次不起作用??
      • 我忘了说,最初password_shown需要是假的。
      • 酷......如果你喜欢它,你可能想“接受”这个答案
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-09
      • 2021-12-01
      相关资源
      最近更新 更多