Android中的getCompoundDrawables()是什么?

时间:2015-10-03 03:31:08

标签: android android-drawable

如何在Android中使用getCompoundDrawables()

if(mEditText.getCompoundDrawables()[2] == null)
    mEditText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_face, 0);

我想检查一下EditText中是否有一个drawable。 if条件是否给出了正确的结果?

2 个答案:

答案 0 :(得分:2)

是的,这是找出是否设置了复合drawable的正确方法。

答案 1 :(得分:0)

是的,你可以。

Drawable drawable =  getActivity().getResources().getDrawable(R.drawable.ic_face);
drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));
if(mEditText.getCompoundDrawables()[2] == null)
 {
    mEditText.setCompoundDrawables(null, null, drawable, null);
 }
mEditText.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Drawable co =  v.getCompoundDrawables()[2];
        if (co == null) {
            return false;
        }
        if (event.getAction() != MotionEvent.ACTION_DOWN) {
            return false;
        }
        if (event.getX() > v.getMeasuredWidth() - v.getPaddingRight()
                - co.getIntrinsicWidth()) {
            //code for right drawable event
            return true;
        } else {
            return false;
        }
    }
});