设置onClickListner的一个EditText的绘制对象权对象、onClickListner、EditText

2023-09-12 23:38:15 作者:༺♛柠栀♛༻

在我的应用程序,我有一个的EditText 与右侧的搜索图标。我用下面给出的code。

 <的EditText
        机器人:ID =@ + ID /搜索
        机器人:layout_width =0dp
        机器人:layout_height =match_parent
        机器人:layout_margin =4dip
        机器人:layout_weight =1
        机器人:背景=@可绘制/ textfield_search1
        机器人:drawableLeft =@可绘制/标志
        机器人:drawableRight =@可绘制/ search_icon
        机器人:提示=搜索什么......
        机器人:填充=4dip
        机器人:单线=真/>
 

我想设置 onClickListner 分配给正确绘制的搜索图标的图像 。对的EditText 它是如何可能的?

解决方案

 公共类CustomEditText扩展的EditText {

    私人绘制对象drawableRight;
    私人绘制对象drawableLeft;
    私人绘制对象drawableTop;
    私人绘制对象drawableBottom;

    诠释actionX,actionY;

    私人DrawableClickListener clickListener;

    公共CustomEditText(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        //当您使用这个观点在XML中需要此Contructure
    }

    公共CustomEditText(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
    }

    保护无效的OnDraw(帆布油画){
        super.onDraw(画布);

    }

    @覆盖
    保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
        super.onSizeChanged(W,H,oldw,oldh);
    }

    @覆盖
    公共无效setCompoundDrawables(可绘制左侧,可绘制前,
            可绘制权,可绘制底部){
        如果(左!= NULL){
            drawableLeft =左;
        }
        如果(右!= NULL){
            drawableRight =权利;
        }
        如果(顶!= NULL){
            drawableTop =顶部;
        }
        如果(底!= NULL){
            drawableBottom =底部;
        }
        super.setCompoundDrawables(左,上,右,下);
    }

    @覆盖
    公共布尔的onTouchEvent(MotionEvent事件){
        矩形边界;
        如果(event.getAction()== MotionEvent.ACTION_DOWN){
            actionX =(int)的event.getX();
            actionY =(int)的event.getY();
            如果(drawableBottom!= NULL
                    &功放;&安培; drawableBottom.getBounds()。包括(actionX,actionY)){
                clickListener.onClick(DrawablePosition.BOTTOM);
                返回super.onTouchEvent(事件);
            }

            如果(drawableTop!= NULL
                    &功放;&安培; drawableTop.getBounds()。包括(actionX,actionY)){
                clickListener.onClick(DrawablePosition.TOP);
                返回super.onTouchEvent(事件);
            }

            //这个工程以来集装箱股份留给0,0起源与边界
            如果(drawableLeft!= NULL){
                边界= NULL;
                边界= drawableLeft.getBounds();

                INT X,Y;
                INT extraTapArea =(int)的(13 * getResources()getDisplayMetrics()密度+0.5);

                X = actionX;
                Y = actionY;

                如果(!bounds.contains(actionX,actionY)){
                    / **给人的+20面积为攻。 * /
                    X =(int)的(actionX  -  extraTapArea);
                    Y =(INT)(actionY  -  extraTapArea);

                    如果(X LT; = 0)
                        X = actionX;
                    如果(γ&所述; = 0)
                        Y = actionY;

                    / **创建从最小值平方* /
                    如果(X LT; y)的{
                        Y = X;
                    }
                }

                如果(bounds.contains(X,Y)及&安培;!clickListener =空){
                    clickListener
                            .onClick(DrawableClickListener.DrawablePosition.LEFT);
                    event.setAction(MotionEvent.ACTION_CANCEL);
                    返回false;

                }
            }

            如果(drawableRight!= NULL){

                边界= NULL;
                边界= drawableRight.getBounds();

                INT X,Y;
                INT extraTapArea = 13;

                / **
                 *如果用户点击刚刚走出一侧的可绘制的矩形
                 *不是添加X AND减去y随一定的价值,以致在
                 *计算X和Y坐标谎言INTO THE DRAWBABLE
                 * 界。 - 这个过程有利于增加的tappable区
                 *矩形。
                 * /
                X =(int)的(actionX + extraTapArea);
                Y =(INT)(actionY  -  extraTapArea);

                / **由于这是从一宽度可绘制减去x的值
                *的看法。使宽度 -  tappedarea将导致x坐标中可绘制的结合。
                * /
                X =的getWidth() -  X;

                 如果用户点击的x坐标只是宽度近/ * x可能为负。
                 *如意见宽度= 300和用户水龙头290然后按previous计算
                 * 290 + 13 = 303。所以减去x上的getWidth()将导致负值。
                 *因此,为了避免这个附加价值previous增加,当x为负。
                 * /

                如果(X LT; = 0){
                    X + = extraTapArea;
                }

                 / *如果计算额外tappable区域后,结果为阴性。
                 *使后减去分配的原始值
                 * extratapping面积值不会进入负值。
                 * /

                如果(γ&所述; = 0)
                    Y = actionY;

                / **如果drawble范围包含x和y的点,然后继续前进。* /
                如果(bounds.contains(X,Y)及&安培;!clickListener =空){
                    clickListener
                            .onClick(DrawableClickListener.DrawablePosition.RIGHT);
                    event.setAction(MotionEvent.ACTION_CANCEL);
                    返回false;
                }
                返回super.onTouchEvent(事件);
            }

        }
        返回super.onTouchEvent(事件);
    }

    @覆盖
    保护无效的finalize()抛出的Throwable
        drawableRight = NULL;
        drawableBottom = NULL;
        drawableLeft = NULL;
        drawableTop = NULL;
        super.finalize();
    }

    公共无效setDrawableClickListener(DrawableClickListener监听器){
        this.clickListener =侦听器;
    }

}
 
码字利器 HHKB静音蓝牙键盘

另外创建与接口

 公共接口DrawableClickListener {

    公共静态枚举DrawablePosition {上,下,左,右};
    公共无效的onClick(DrawablePosition目标);
    }
 

不过如果u需要任何帮助,评论

另外设置drawableClickListener在活动文件中的视图。

  editText.setDrawableClickListener(新DrawableClickListener(){


        公共无效的onClick(DrawablePosition目标){
            开关(目标){
            情况下左:
                //做的东西在这里
                打破;

            默认:
                打破;
            }
        }

    });
 

In my app i have a EditText with a search Icon on the right side. I used the code given below.

 <EditText
        android:id="@+id/search"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="4dip"
        android:layout_weight="1"
        android:background="@drawable/textfield_search1"
        android:drawableLeft="@drawable/logo"
        android:drawableRight="@drawable/search_icon"
        android:hint="Search Anything..."
        android:padding="4dip"
        android:singleLine="true" />

I want to set the onClickListner for the search icon image assigned to the right drawable of EditText. How it is possible ?

解决方案

public class CustomEditText extends EditText {

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

    int actionX, actionY;

    private DrawableClickListener clickListener;

    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) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    public void setCompoundDrawables(Drawable left, Drawable top,
            Drawable right, Drawable bottom) {
        if (left != null) {
            drawableLeft = left;
        }
        if (right != null) {
            drawableRight = right;
        }
        if (top != null) {
            drawableTop = top;
        }
        if (bottom != null) {
            drawableBottom = bottom;
        }
        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(DrawablePosition.BOTTOM);
                return super.onTouchEvent(event);
            }

            if (drawableTop != null
                    && drawableTop.getBounds().contains(actionX, actionY)) {
                clickListener.onClick(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;
    }

}

Also Create an Interface with

public interface DrawableClickListener {

    public static enum DrawablePosition { TOP, BOTTOM, LEFT, RIGHT };
    public void onClick(DrawablePosition target); 
    }

Still if u need any help, comment

Also set the drawableClickListener on the view in activity file.

editText.setDrawableClickListener(new DrawableClickListener() {


        public void onClick(DrawablePosition target) {
            switch (target) {
            case LEFT:
                //Do something here
                break;

            default:
                break;
            }
        }

    });