Android自定义View软键盘实现搜索
作者:路边桥凉 时间:2022-02-10 07:22:45
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软键盘实现搜索,希望大家喜欢。
标签:android,自定义,view,软键盘,搜索
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Flutter Widgets之标签类控件Chip详解
2023-06-26 14:22:35
![](https://img.aspxhome.com/file/2023/0/120090_0s.jpg)
SpringBoot搭建多数据源的实现方法
2022-07-02 18:57:04
![](https://img.aspxhome.com/file/2023/2/91542_0s.png)
解决springboot配置logback-spring.xml不起作用问题
2022-09-10 11:21:24
![](https://img.aspxhome.com/file/2023/8/64488_0s.png)
详解Android的网络数据存储
2022-08-22 01:39:39
详解Java如何在Array和List之间进行转换
2023-05-27 07:37:27
![](https://img.aspxhome.com/file/2023/9/95029_0s.jpg)
Java char[]数组转成String类型详细介绍
2021-09-27 22:18:49
![](https://img.aspxhome.com/file/2023/4/90994_0s.jpg)
Java生成随机姓名、性别和年龄的实现示例
2023-11-13 16:43:26
![](https://img.aspxhome.com/file/2023/7/59257_0s.png)
java实现双色球彩票游戏
2022-06-29 12:39:45
![](https://img.aspxhome.com/file/2023/2/66652_0s.jpg)
JavaMail入门教程之发送邮件(3)
2023-02-01 20:01:16
![](https://img.aspxhome.com/file/2023/5/71215_0s.jpg)
Spring Boot 集成 Sharding-JDBC + Mybatis-Plus 实现分库分表功能
2023-08-28 16:52:09
![](https://img.aspxhome.com/file/2023/7/58277_0s.jpg)
Java 数据结构与算法系列精讲之KMP算法
2023-05-06 14:55:55
![](https://img.aspxhome.com/file/2023/7/64907_0s.jpg)
Java异常 Factory method'sqlSessionFactory'rew exception;ested exception is java.lang.NoSuchMethodError:
2022-03-25 15:06:42
![](https://img.aspxhome.com/file/2023/8/86818_0s.jpg)
Spring boot + mybatis + orcale实现步骤实例代码讲解
2023-07-11 22:50:09
![](https://img.aspxhome.com/file/2023/1/61261_0s.png)
C#中WebClient实现文件下载
2022-10-11 18:04:57
![](https://img.aspxhome.com/file/2023/5/77275_0s.png)
Java实现班级管理系统
2022-05-08 07:29:48
SpringMVC中的Model对象用法说明
2022-12-15 02:24:34
Android实现歌词渐变色和进度的效果
2023-09-24 02:33:17
![](https://img.aspxhome.com/file/2023/8/91708_0s.png)
java 发送http和https请求的实例
2023-11-29 12:46:52
java弱口令检测机制解析
2022-09-27 01:07:06
Spring Retry 重试实例详解
2021-07-15 22:43:26
![](https://img.aspxhome.com/file/2023/5/63495_0s.png)