給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);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.