2017-02-14 47 views
0

我添加了可繪製的左右圖標來編輯文本。可繪製的正確使用來顯示或隱藏密碼,但是當我點擊可繪製的右側圖標時,可繪製的左側變爲不可見。如何解決此問題?可繪製的左側圖標變爲隱形?

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

      if (event.getAction() == MotionEvent.ACTION_UP) { 

       if (event.getRawX() >= (txtPassword.getRight() - txtPassword.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) { 
        if (IsHidePwd) { 
         txtPassword.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_show_pwd, 0); 
         txtPassword.setTransformationMethod(PasswordTransformationMethod.getInstance()); 

         IsHidePwd = false; 
         return true; 
        } else { 
         txtPassword.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_hide_pwd, 0); 
         txtPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); 

         IsHidePwd = true; 
         return true; 
        } 
       } 
      } 
      return false; 
     } 
    }); 
+0

嘗試將大小設置爲txtPassword.setCompoundDrawablesWithIntrinsicBounds(10,10,R.drawable.ic_show_pwd,10); 它適合我。 –

回答

1

在這一行我認爲你需要設置左邊的drawable。

txtPassword.setCompoundDrawablesWithIntrinsicBounds(R.drawable.left_img,0,R.drawable.ic_show_pwd,0);

+0

非常感謝.. !!! –

相關問題