更改编辑文本的可绘制权限

时间:2015-06-02 16:33:14

标签: android onclick android-drawable

我试图更改我的EditText中的drawable,就像它在图片中一样。Please refer to the password column

我使用以下代码使drawable可点击。

 password_editText.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                final int DRAWABLE_LEFT = 0;
                final int DRAWABLE_TOP = 1;
                final int DRAWABLE_RIGHT = 2;
                final int DRAWABLE_BOTTOM = 3;

                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (event.getRawX() >= (password_editText.getRight() - password_editText
                            .getCompoundDrawables()[DRAWABLE_RIGHT].getBounds()
                            .width())) {
                        // your action here
                        Toast.makeText(MainActivity.this, "drawable click",
                                Toast.LENGTH_LONG).show();



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

在此,我想更改密码列中的drawable,以便在单击时更改。

2 个答案:

答案 0 :(得分:0)

您可以使用

在运行时更改drawable
Drawable img = getContext().getResources().getDrawable( R.drawable.your_drawable );

password_editText.setCompoundDrawables( null, null, img, null ); 

答案 1 :(得分:0)

您可以使用setCompoundDrawablesWithIntrinsicBounds函数:

password_editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.yourdrawable, 0);

params的顺序:left,top,right,bottom