【Android自定义View实战】之自定义超简单SearchView搜索框

2024-05-22 07:08

本文主要是介绍【Android自定义View实战】之自定义超简单SearchView搜索框,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

【Android自定义View实战】之自定义超简单SearchView搜索框


这篇文章是对之前文章的翻新,至于为什么我要重新修改这篇文章?原因如下

1.有人举报我抄袭,原文链接:http://www.it165.net/pro/html/201407/17779.html。呵呵...................................................................请大家仔细看看,那个图片水印。到底是谁抄袭谁呢?
2.之前的那篇文章写得非常随意,今天先到来封装一个自定义View,使用起来更方便。

在Android开发中我们经常会用到搜索框,而系统提供的又不尽完美。所以自定义一个比较简单的SearchView。代码非常简单,高手请略过。


效果图



实现代码

1.布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="50dp"android:layout_marginLeft="20dp"android:layout_marginRight="12dp"android:layout_marginTop="5dp"android:background="@drawable/search_bg"android:orientation="horizontal"><Buttonandroid:layout_width="20dp"android:layout_height="20dp"android:layout_gravity="right|center_vertical"android:layout_margin="10dp"android:background="@mipmap/search" /><!-- 输入的搜索信息 --><EditTextandroid:id="@+id/et_search"android:layout_width="0dp"android:layout_height="fill_parent"android:layout_marginLeft="10dp"android:layout_weight="4"android:background="@null"android:gravity="center_vertical"android:hint="输入内容进行搜索"android:imeOptions="actionSearch"android:singleLine="true"android:textColor="#0e0e0e"android:textColorHint="#b0c6ce"android:textSize="17sp" /><Buttonandroid:id="@+id/bt_clear"android:layout_width="25dp"android:layout_height="25dp"android:layout_gravity="right|center_vertical"android:layout_margin="10dp"android:background="@mipmap/delete" /></LinearLayout></LinearLayout>


2.java代码
package cn.bluemobi.dylan.searchview;import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;/*** Android自定义SearchView* Created by yuandl */public class SearchView extends LinearLayout implements TextWatcher, View.OnClickListener {/*** 输入框*/private EditText et_search;/*** 输入框后面的那个清除按钮*/private Button bt_clear;public SearchView(Context context, AttributeSet attrs) {super(context, attrs);/**加载布局文件*/LayoutInflater.from(context).inflate(R.layout.pub_searchview, this, true);/***找出控件*/et_search = (EditText) findViewById(R.id.et_search);bt_clear = (Button) findViewById(R.id.bt_clear);bt_clear.setVisibility(GONE);et_search.addTextChangedListener(this);bt_clear.setOnClickListener(this);}@Overridepublic void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}@Overridepublic void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}/***** 对用户输入文字的监听** @param editable*/@Overridepublic void afterTextChanged(Editable editable) {/**获取输入文字**/String input = et_search.getText().toString().trim();if (input.isEmpty()) {bt_clear.setVisibility(GONE);} else {bt_clear.setVisibility(VISIBLE);}}@Overridepublic void onClick(View view) {et_search.setText("");}
}


3.具体功能的实现
以上只是对界面的写法,下面是对 搜索记录功能实现: 【玩转SQLite系列】(六)SQLite数据库应用案例实现历史搜索记录


GitHub
2.java代码

这篇关于【Android自定义View实战】之自定义超简单SearchView搜索框的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/996014

相关文章

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

Python列表去重的4种核心方法与实战指南详解

《Python列表去重的4种核心方法与实战指南详解》在Python开发中,处理列表数据时经常需要去除重复元素,本文将详细介绍4种最实用的列表去重方法,有需要的小伙伴可以根据自己的需要进行选择... 目录方法1:集合(set)去重法(最快速)方法2:顺序遍历法(保持顺序)方法3:副本删除法(原地修改)方法4:

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

在Spring Boot中浅尝内存泄漏的实战记录

《在SpringBoot中浅尝内存泄漏的实战记录》本文给大家分享在SpringBoot中浅尝内存泄漏的实战记录,结合实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录使用静态集合持有对象引用,阻止GC回收关键点:可执行代码:验证:1,运行程序(启动时添加JVM参数限制堆大小):2,访问 htt

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(