使用Edittext实现搜索视图的功能

问题描述 投票:0回答:1

我正在一个购物项目中,我想在EditText中创建动态的结束图标,当用户专注于EditText时,该图标会发生变化。

因此,基本上,在用户与editText交互之前,该图标在右侧显示“搜索”图标,而当用户单击并输入一些信息时,该图标将显示“清除”图标。

我仅实现了可绘制对象的可见功能。

edtSearch.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b)
            {
                if(b){
                    edtSearch.setCompoundDrawablesWithIntrinsicBounds(0,0,0,0);
                    edtSearch.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.ic_cancel_black_24dp,0);
                }
            }
        });

糟糕!如何处理搜索EditText的取消单击?

java android xml search
1个回答
1
投票

也许这可以帮助您

public class MyEditText extends EditText {

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

int actionX, actionY;

private DClickListener clickListener;

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

public MyEditText(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(DClickListener.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(DClickListener.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(DListener listener) {
    this.clickListener = listener;
}

} 


public interface DClickListener {

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

在活动中,您可以添加以下代码,其中edtSe​​arch是MyEditText对象

edtSearch.setDrawableClickListener(new DClickListener() {


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

        default:
            break;
        }
    }

});

使用这样的Xml

  <yourPackageName.MyEditText
        android:id="@+id/imgSearch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
          />
© www.soinside.com 2019 - 2024. All rights reserved.