搜索的自定义控件

114319_OfRg_2902213.png

xml文件

<com.cqgk.agricul.view.MyDemonstrationSearchView
    android:id="@+id/whole_product_search"
    android:layout_width="match_parent"
    android:layout_height="@dimen/y39"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="@dimen/y11"
    android:layout_marginLeft="@dimen/x24"
    android:layout_marginRight="@dimen/x24"
    android:layout_marginTop="@dimen/y11"
    android:background="@drawable/bg_gray_round"
    android:gravity="center"
    android:hint="搜索"
    android:singleLine="true"
    android:textSize="@dimen/x19"
    attr:imageheight="@dimen/y19"
    attr:imagewidth="@dimen/x18"
    attr:textSize="@dimen/x19"/>

 

自定义代码

package com.cqgk.agricul.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.EditText;

import com.cqgk.agricul.R;

/**
 * 创建者     fx
 * 创建时间   

 * 保留所有权利。
 * <p/>

 * 您不得披露该保密信息,并只能按照您与深圳xxx有限公司签订的许可协议中的条款使用。
 * <p/>
 * 更新者     ${Author}
 * 更新时间   2016/12/16 11:24
 * 更新描述   ${TODO}
 */
public class MyDemonstrationSearchView extends EditText {


    float searchSize = 0;
    float searchSizeHeight = 0;
    float textSize = 0;
    int textColor = 0xFF000000;
    Drawable mDrawable;
    Paint paint;

    public MyDemonstrationSearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        InitResource(context, attrs);
        InitPaint();
    }

    private void InitResource(Context context, AttributeSet attrs) {
        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.searchedit);
        float density = context.getResources().getDisplayMetrics().density;
        searchSize = mTypedArray.getDimension(R.styleable.searchedit_imagewidth, 18 * density + 0.5F);
        searchSizeHeight = mTypedArray.getDimension(R.styleable.searchedit_imageheight, 18 * density + 0.5F);


        textColor = mTypedArray.getColor(R.styleable.searchedit_textColor, 0xFF848484);
        textSize = mTypedArray.getDimension(R.styleable.searchedit_textSize, 14 * density + 0.5F);
        mTypedArray.recycle();
    }

    private void InitPaint() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(textColor);
        paint.setTextSize(textSize);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        DrawSearchIcon(canvas);
    }

    private void DrawSearchIcon(Canvas canvas) {
        if (this.getText().toString().length() == 0) {
            //            float textWidth = paint.measureText("搜索您喜欢的商品");
            float textWidth = paint.measureText("搜索");
            float textHeight = getFontLeading(paint);

            float dx = (getWidth() - searchSize - textWidth - 8) / 2;
            float dy = (getHeight() - searchSize) / 2;

            canvas.save();
            canvas.translate(getScrollX() + dx - 40, getScrollY() + dy);
            if (mDrawable != null) {
                mDrawable.draw(canvas);
            }
            canvas.drawText("搜索", getScrollX() + searchSize + 8, getScrollY() + (getHeight() - (getHeight() - textHeight) / 2) - paint.getFontMetrics().bottom - dy, paint);
            //            canvas.drawText("搜索您喜欢的商品", getScrollX() + searchSize + 8, getScrollY() + (getHeight() - (getHeight() - textHeight) / 2) - paint.getFontMetrics().bottom - dy, paint);
            canvas.restore();
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (mDrawable == null) {
            try {
                mDrawable = getContext().getResources().getDrawable(R.drawable.icon_search_et);
                mDrawable.setBounds(0, 0, (int) searchSize, (int) searchSizeHeight);
            } catch (Exception e) {

            }
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        if (mDrawable != null) {
            mDrawable.setCallback(null);
            mDrawable = null;
        }
        super.onDetachedFromWindow();
    }

    public float getFontLeading(Paint paint) {
        Paint.FontMetrics fm = paint.getFontMetrics();
        return fm.bottom - fm.top;
    }

}

 

下面是对搜索内容输入之后的enter键监听获取数据

MyDemonstrationSearchView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
        if(actionId== EditorInfo.IME_ACTION_DONE){//点击的enter键
            String text = v.getText().toString();
            Toast.makeText(getApplicationContext(), "这里对搜索内容"+text+"=====", Toast.LENGTH_SHORT).show();
            //在这里发起post请求,传入搜索输入的内容,然后重新再一个位置重新显示
        }
        return false;
    }
});

转载于:https://my.oschina.net/fxflysky/blog/806943

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值