欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中搜索图标和文字居中的EditText实例

程序员文章站 2023-11-16 20:37:58
效果图: 需要自定义view,具体实现如下: import android.widget.edittext; import android.conte...

效果图:

Android中搜索图标和文字居中的EditText实例

需要自定义view,具体实现如下:

import android.widget.edittext;

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 com.example.administrator.mahu.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.mipmap.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;
  }

}

在values---attrs下添加

<declare-styleable name="searchedit">
    <attr name="imagewidth" format="dimension" />
    <attr name="textsize" format="dimension" />
    <attr name="textcolor" format="color" />
 </declare-styleable>

搜索图片

Android中搜索图标和文字居中的EditText实例

在布局文件中调用如下

<com.example.administrator.mahu.view.searchview
    android:id="@+id/search"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_below="@+id/layout"
    android:background="@drawable/search_kuang"
    android:textsize="17sp"
    android:paddingleft="5dp"
    android:singleline="true"
    android:imeoptions="actionsearch"
    />

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。