给EditText左右两边设置图片与监听(drawableLeft/drawableRight)/弹出键盘

设置图片:

Drawable phoneDrawableLeft = getResources().getDrawable(R.mipmap.phone_login, null);
phoneDrawableLeft.setBounds(0, 0, drawableDimension, drawableDimension);

Drawable phoneDrawableRight = getResources().getDrawable(R.mipmap.empty, null);
phoneDrawableRight.setBounds(0, 0, drawableDimension, drawableDimension);

mEtPhone.setCompoundDrawables(phoneDrawableLeft, null, phoneDrawableRight, null);
mEtPhone.setCompoundDrawablePadding(Utils.dp2px(this, 5));



设置监听:自定义View集成EditText,然后重写onTouchEvent方法,注意getX()获取的是View在屏幕X轴的坐标,不是相对坐标:

public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_UP:
            Drawable right = getCompoundDrawables()[2];  //获取右边图片
            Drawable left = getCompoundDrawables()[0];  //获取左边图片

            if ((right != null) && (event.getRawX() >= (getX() + (getWidth() - right.getBounds().width()))) && mDrawableRightListener != null) {
                mDrawableRightListener.onDrawableClick();
                return true;
            }

            if ((left != null) && (event.getRawX() <= (getX() + left.getBounds().width())) && mDrawableLeftListener != null) {
                mDrawableLeftListener.onDrawableClick();
                return true;
            }
            break;
    }
    return super.onTouchEvent(event);
}

public void setDrawableRightListener(DrawableListener l) {
    mDrawableRightListener = l;
}

public void setDrawableLeftListener(DrawableListener l) {
    mDrawableLeftListener = l;
}

public interface DrawableListener {
    public void onDrawableClick();
}



设置焦点变化监听:

//设置焦点变化监听
mEtPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus){
            mEtPhone.setCompoundDrawables(phoneDrawableLeft, null, phoneDrawableRight, null);
        } else {
            mEtPhone.setCompoundDrawables(phoneDrawableLeft, null, null, null);
        }
    }
});



自动弹出键盘:

//弹出键盘
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

你可能感兴趣的:(android,xMusic开发笔记)