gpt4 book ai didi

Android自定义View软键盘实现搜索

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Android自定义View软键盘实现搜索由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

1. xml文件中加入自定义 搜索view 。

?
1
2
3
4
5
<com.etoury.etoury.ui.view.iconcenteredittext
       android:id= "@+id/search_et"
       style= "@style/styleedittext"
       android:hint= "搜索景点信息"
       />

2. 自定义的   view java文件 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</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 。

?
1
2
3
4
5
6
7
8
<?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 中加上代码 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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软键盘实现搜索,希望大家喜欢.

最后此篇关于Android自定义View软键盘实现搜索的文章就讲到这里了,如果你想了解更多关于Android自定义View软键盘实现搜索的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com