3

我在我的 android 应用程序中创建了一个 CustomeEditText,用于在右侧 Drawable 上分配一个可绘制点击。

课程如下。

public class CustomEditText extends EditText {

private Drawable drawableRight;
private Drawable drawableLeft;
private Drawable drawableTop;
private Drawable drawableBottom;

int actionX, actionY;
int mHeight = 0;

private DrawableClickListener clickListener;

    public CustomEditText(Context context) {
    super(context);
}

public CustomEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    // this Contructure required when you are using this view in xml
}

public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    mHeight = h;
    super.onSizeChanged(w, h, oldw, oldh);
}

@Override
public void setCompoundDrawables(Drawable left, Drawable top,
        Drawable right, Drawable bottom) {
    Log.v("Login", "setCompoundDrawables");
    if (left != null) {
        Log.v("Login", "Left Not Null");
        drawableLeft = left;
    }
    if (right != null) {
        drawableRight = right;
    }
    if (top != null) {
        drawableTop = top;
    }
    if (bottom != null) {
        drawableBottom = bottom;
    }
    Log.v("CustomeEditText", "Left :"+left);

    super.setCompoundDrawables(left, top, right, bottom);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    Rect bounds;
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        actionX = (int) event.getX();
        actionY = (int) event.getY();
        if (drawableBottom != null
                && drawableBottom.getBounds().contains(actionX, actionY)) {
            clickListener.onClick(DrawableClickListener.DrawablePosition.BOTTOM);
            return super.onTouchEvent(event);
        }

        if (drawableTop != null
                && drawableTop.getBounds().contains(actionX, actionY)) {
            clickListener.onClick(DrawableClickListener.DrawablePosition.TOP);
            return super.onTouchEvent(event);
        }

        // this works for left since container shares 0,0 origin with bounds
        if (drawableLeft != null) {
            bounds = null;
            bounds = drawableLeft.getBounds();

            int x, y;
            int extraTapArea = (int) (13 * getResources()
                    .getDisplayMetrics().density + 0.5);

            x = actionX;
            y = actionY;

            if (!bounds.contains(actionX, actionY)) {
                /** Gives the +20 area for tapping. */
                x = (int) (actionX - extraTapArea);
                y = (int) (actionY - extraTapArea);

                if (x <= 0)
                    x = actionX;
                if (y <= 0)
                    y = actionY;

                /** Creates square from the smallest value */
                if (x < y) {
                    y = x;
                }
            }

            if (bounds.contains(x, y) && clickListener != null) {
                clickListener
                        .onClick(DrawableClickListener.DrawablePosition.LEFT);
                event.setAction(MotionEvent.ACTION_CANCEL);
                return false;

            }
        }

        if (drawableRight != null) {

            bounds = null;
            bounds = drawableRight.getBounds();

            int x, y;
            int extraTapArea = 13;

            /**
             * IF USER CLICKS JUST OUT SIDE THE RECTANGLE OF THE DRAWABLE
             * THAN ADD X AND SUBTRACT THE Y WITH SOME VALUE SO THAT AFTER
             * CALCULATING X AND Y CO-ORDINATE LIES INTO THE DRAWBABLE
             * BOUND. - this process help to increase the tappable area of
             * the rectangle.
             */
            x = (int) (actionX + extraTapArea);
            y = (int) (actionY - extraTapArea);

            /**
             * Since this is right drawable subtract the value of x from the
             * width of view. so that width - tappedarea will result in x
             * co-ordinate in drawable bound.
             */
            x = getWidth() - x;

            /*
             * x can be negative if user taps at x co-ordinate just near the
             * width. e.g views width = 300 and user taps 290. Then as per
             * previous calculation 290 + 13 = 303. So subtract X from
             * getWidth() will result in negative value. So to avoid this
             * add the value previous added when x goes negative.
             */

            if (x <= 0) {
                x += extraTapArea;
            }

            /*
             * If result after calculating for extra tappable area is
             * negative. assign the original value so that after subtracting
             * extratapping area value doesn't go into negative value.
             */

            if (y <= 0)
                y = actionY;

            /**
             * If drawble bounds contains the x and y points then move
             * ahead.
             */
            if (bounds.contains(x, y) && clickListener != null) {
                clickListener
                        .onClick(DrawableClickListener.DrawablePosition.RIGHT);
                event.setAction(MotionEvent.ACTION_CANCEL);
                return false;
            }
            return super.onTouchEvent(event);
        }

    }
    return super.onTouchEvent(event);
}

@Override
protected void finalize() throws Throwable {
    drawableRight = null;
    drawableBottom = null;
    drawableLeft = null;
    drawableTop = null;
    super.finalize();
}

public void setDrawableClickListener(DrawableClickListener listener) {
    this.clickListener = listener;
}

public interface DrawableClickListener {

    public static enum DrawablePosition {
        TOP, BOTTOM, LEFT, RIGHT
    };

    public void onClick(DrawablePosition target);
}

}

所以在xml中

<com.mypack.CustomEditText
        android:id="@+id/category"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_margin="2dp"
        android:background="@drawable/b"
        android:drawablePadding="10dp"
        android:drawableRight="@drawable/category"
        android:hint="My hint"
        android:padding="10dp"
        android:textColor="#000000"
        android:textSize="15sp"
        android:textStyle="bold" />

现在我想通过代码创建相同的视图。

我试图像这样创建

CustomEditText example = new CustomEditText(this);
    Log.v("Login", "message");
    example.setCompoundDrawables(getResources().getDrawable(R.drawable.delete_category), null, null, null);
    example.setBackgroundResource(R.drawable.category_background);
    example.setGravity(Gravity.CENTER);
    example.setTextColor(getResources().getColor(
            android.R.color.white));
    //example.setMargins(4, 4, 4, 4);
    example.setPadding(4, 4, 4, 4);
    example.setTextSize(20);
    example.setText(category);
    ll.addView(example);

它正在创建一个在左侧没有可绘制的普通编辑文本。我如何在右侧创建一个带有可绘制的 CustomeEditText。

4

2 回答 2

1

你应该使用

example.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.delete_category), null, null, null);

代替

example.setCompoundDrawables(getResources().getDrawable(R.drawable.delete_category), null, null, null);

这将导致您的自定义EditTextDrawable左侧。

所以你的代码会是这样的:

CustomEditText example = new CustomEditText(this);
    Log.v("Login", "message");
    example.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.delete_category), null, null, null);
    example.setBackgroundResource(R.drawable.category_background);
    example.setGravity(Gravity.CENTER);
    example.setTextColor(getResources().getColor(
            android.R.color.white));
    //example.setMargins(4, 4, 4, 4);
    example.setPadding(4, 4, 4, 4);
    example.setTextSize(20);
    example.setText(category);
    ll.addView(example);

1. setCompoundDrawable 添加Drawables:

如果要使用setCompoundDrawable添加,Drawables则需要使用 setBounds 指定边界。这将在图像周围定义一个边界矩形。如果你不这样做,你将无法看到Drawable.

例如。在你的情况下使用它,

Drawable img = getResources().getDrawable(R.drawable.delete_category);
img.setBounds( 0, 0, 60, 60 );
example.setCompoundDrawables(img, null, null, null);

2. setCompoundDrawableWithIntrinsicBounds 添加Drawables:

你可以setCompoundDrawableWithIntrinsicBounds()改用。它将使用图像的大小作为Drawable. 即 Drawables 的边界将被设置为它们的内在边界。

于 2013-09-10T09:50:33.520 回答
-1

机器人:重力=“对”

把它放在你的 xml 或 setGravity(Gravity.RIGHT);

于 2013-09-10T09:13:00.453 回答