Android自定义View软键盘实现搜索
程序员文章站
2023-12-12 13:25:10
1. xml文件中加入自定义 搜索view
1. xml文件中加入自定义 搜索view
<com.etoury.etoury.ui.view.iconcenteredittext android:id="@+id/search_et" style="@style/styleedittext" android:hint="搜索景点信息" />
2. 自定义的 view java文件
iconcenteredittext.java package com.etoury.etoury.ui.view; import android.content.context; import android.graphics.canvas; import android.graphics.drawable.drawable; import android.text.textutils; import android.util.attributeset; import android.util.log; import android.view.keyevent; import android.view.view; import android.view.inputmethod.inputmethodmanager; import android.widget.edittext; public class iconcenteredittext extends edittext implements view.onfocuschangelistener, view.onkeylistener { private static final string tag = iconcenteredittext.class.getsimplename(); /** * 是否是默认图标再左边的样式 */ private boolean isleft = false; /** * 是否点击软键盘搜索 */ private boolean presssearch = false; /** * 软键盘搜索键监听 */ private onsearchclicklistener listener; public void setonsearchclicklistener(onsearchclicklistener listener) { this.listener = listener; } public iconcenteredittext(context context) { this(context, null); init(); } public iconcenteredittext(context context, attributeset attrs) { this(context, attrs, android.r.attr.edittextstyle); init(); } public iconcenteredittext(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); init(); } private void init() { setonfocuschangelistener(this); setonkeylistener(this); } @override protected void ondraw(canvas canvas) { if (isleft) { // 如果是默认样式,则直接绘制 super.ondraw(canvas); } else { // 如果不是默认样式,需要将图标绘制在中间 drawable[] drawables = getcompounddrawables(); drawable drawableleft = drawables[0]; drawable drawableright = drawables[2]; translate(drawableleft, canvas); translate(drawableright, canvas); // if (drawableleft != null) { // float textwidth = getpaint().measuretext(gethint().tostring()); // int drawablepadding = getcompounddrawablepadding(); // int drawablewidth = drawableleft.getintrinsicwidth(); // float bodywidth = textwidth + drawablewidth + drawablepadding; // // canvas.translate((getwidth() - bodywidth - getpaddingleft() - getpaddingright()) / 2, 0); // } // if (drawableright != null) { // float textwidth = getpaint().measuretext(gethint().tostring()); // 文字宽度 // int drawablepadding = getcompounddrawablepadding(); // 图标间距 // int drawablewidth = drawableright.getintrinsicwidth(); // 图标宽度 // float bodywidth = textwidth + drawablewidth + drawablepadding; // setpadding(getpaddingleft(), getpaddingtop(), (int)(getwidth() - bodywidth - getpaddingleft()), getpaddingbottom()); // canvas.translate((getwidth() - bodywidth - getpaddingleft()) / 2, 0); // } super.ondraw(canvas); } } public void translate(drawable drawable, canvas canvas) { if (drawable != null) { float textwidth = getpaint().measuretext(gethint().tostring()); int drawablepadding = getcompounddrawablepadding(); int drawablewidth = drawable.getintrinsicwidth(); float bodywidth = textwidth + drawablewidth + drawablepadding; if (drawable == getcompounddrawables()[0]) { canvas.translate((getwidth() - bodywidth - getpaddingleft() - getpaddingright()) / 2, 0); } else { setpadding(getpaddingleft(), getpaddingtop(), (int)(getwidth() - bodywidth - getpaddingleft()), getpaddingbottom()); canvas.translate((getwidth() - bodywidth - getpaddingleft()) / 2, 0); } } } @override public void onfocuschange(view v, boolean hasfocus) { log.d(tag, "onfocuschange execute"); // 恢复edittext默认的样式 if (!presssearch && textutils.isempty(gettext().tostring())) { isleft = hasfocus; } } @override public boolean onkey(view v, int keycode, keyevent event) { presssearch = (keycode == keyevent.keycode_enter); if (presssearch && listener != null) { /*隐藏软键盘*/ inputmethodmanager imm = (inputmethodmanager) v.getcontext().getsystemservice(context.input_method_service); if (imm.isactive()) { imm.hidesoftinputfromwindow(v.getapplicationwindowtoken(), 0); } listener.onsearchclick(v); } return false; } public interface onsearchclicklistener { void onsearchclick(view view); } }
3. style
</style> <style name="styleedittext"> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:background">@drawable/bg_search_bar</item> <item name="android:drawablepadding">5dp</item> <item name="android:gravity">center_vertical</item> <item name="android:imeoptions">actionsearch</item> <item name="android:drawableleft">@drawable/icon_search</item> <item name="android:padding">5dp</item> <item name="android:singleline">true</item> <item name="android:textcolorhint">@color/grey</item> <item name="android:textsize">16sp</item> <item name="android:hint">搜索</item> </style>
4. bg_search_bar.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@android:color/white" /> <stroke android:width="1px" android:color="@android:color/darker_gray" /> <corners android:radius="3dp" /> </shape>
5. activity 中加上代码
private iconcenteredittext search_et; search_et = (iconcenteredittext) findviewbyid(r.id.search_et); search_et.setonsearchclicklistener(new onsearchclicklistener() { @override public void onsearchclick(view view) { // todo auto-generated method stub string texts = search_et.gettext().tostring().trim(); if ("".equals(texts)) { toastutil.showtoast("请输入您要搜索的内容"); } else { //根据你的文字内容实现跳转 intent intent = new intent(context, searchwordactivity.class); // intent.putextra("searchmode", 1); intent.putextra("searchword", texts); context.startactivity(intent); } } });
以上内容是小编给大家介绍的android自定义view软键盘实现搜索,希望大家喜欢。