服务器之家

服务器之家 > 正文

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

时间:2021-04-19 15:10     来源/作者:路边桥凉

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软键盘实现搜索,希望大家喜欢。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部