Android EditText搜索框实现图标居中
程序员文章站
2022-10-29 14:48:27
类似这样edittext 搜索框,hiht 提示有一个icon并且text内容。
重写edittext :
package mobi.truekey...
类似这样edittext 搜索框,hiht 提示有一个icon并且text内容。
重写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; } }
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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
Android EditText搜索框实现图标居中
-
Android ListView用EditText实现搜索功能效果
-
Android EditText输入框实现下拉且保存最近5个历史记录思路详解
-
Android 登录页面的实现代码(密码显示隐藏、EditText 图标切换、限制输入长度)
-
Android EditText输入框实现下拉且保存最近5个历史记录
-
Android 支付密码输入框,自定义EditText实现密码输入框功能;
-
Android自定义View实现搜索框(SearchView)功能
-
Android EditText搜索框实现图标居中
-
Android|Toast理论篇(附自制图标信息框实现)
-
Android EditText输入框实现下拉且保存最近5个历史记录思路详解