效果如图,搜索框带有搜索的图片。
代码如下
/** * Created by 海峰 on 2016/10/18. */ import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.drawable.Drawable; import android.support.v4.content.ContextCompat; import android.util.AttributeSet; import android.widget.EditText; import com.hoge.app4hrzki6lz7.R; public class SeachView extends EditText { private float searchSize = 0; private float textSize = 0; private int textColor = 0xFF000000; private Drawable mDrawable; private Paint paint; public SeachView(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, 15 * 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 textHeight = getFontLeading(paint); float dx = (getWidth() - searchSize - textWidth - 15) / 2; float dy = (getHeight() - searchSize) / 2; canvas.save(); canvas.translate(getScrollX() + dx, getScrollY() + dy); if (mDrawable != null) { mDrawable.draw(canvas); } canvas.drawText("艺术/标题/新闻", getScrollX() + searchSize + 15, getScrollY() + (getHeight() - (getHeight() - textHeight) / 2) - paint.getFontMetrics().bottom - dy, paint); canvas.restore(); } } //初始化搜索图片的大小 @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); if (mDrawable == null) { try { mDrawable = ContextCompat.getDrawable(getContext(),R.mipmap.seach); mDrawable.setBounds(0, 0, (int) searchSize, (int) searchSize); } 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; } }用法如下
<com.wbkj.artmss.Weight.SeachView android:id="@+id/et_seach" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/edit_common_yuanjiao" android:imeOptions="actionSearch" android:paddingBottom="5dp" android:paddingLeft="10dp" android:paddingTop="5dp" android:textColorHint="@color/hint" />然后在activity里设置输入框默认搜索功能seach.setImeOptions(EditorInfo.IME_ACTION_SEARCH); seach.setInputType(EditorInfo.TYPE_CLASS_TEXT); seach.setSingleLine(true); seach.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH) { if (!getSeachString().equals("")){ page=0; getResultData(getSeachString()); //调服务器 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); //隐藏键盘 imm.hideSoftInputFromWindow(v.getWindowToken(), 0); }else{ Toast.makeText(SearchActivity.this,"搜索内容不能为空",Toast.LENGTH_SHORT).show(); } } return false; } });