Android EditText 검색 상자 에서 아이콘 가운데 구현

이와 같이 EditText 검색 상자 에서 hiht 알림 은 icon 과 text 내용 이 있 습 니 다.


편집 텍스트 다시 쓰기:

package mobi.truekey.weapp2.widget;

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 mobi.truekey.weapp2.R;

public class SearchView extends EditText {

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

  public SearchView(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);
    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 - 8) / 2;
      float dy = (getHeight() - searchSize) / 2;

      canvas.save();
      canvas.translate(getScrollX() + dx, getScrollY() + dy);
      if (mDrawable != null) {
        mDrawable.draw(canvas);
      }
      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.search);
        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;
  }

}
attr:

<declare-styleable name="searchedit">
  <attr name="imagewidth" format="dimension" />
  <attr name="textSize" format="dimension" />
  <attr name="textColor" format="color" />
</declare-styleable>
drawable 배경:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <corners android:radius="6dp" />
  <solid android:color="@color/white" />

</shape>
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기