资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

AndroidEditText搜索框实现图标居中-创新互联

类似这样EditText 搜索框,hiht 提示有一个icon并且text内容。

穆棱ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!

重写EditText :


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;
  }

}

网站栏目:AndroidEditText搜索框实现图标居中-创新互联
转载注明:http://cdkjz.cn/article/doheio.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220