仿IOS搜索框简单实现
2016-06-03 10:45
435 查看
先上个图
具体实现:
1.主类:
2.Layout.xml
3.背景
使用:
具体实现:
1.主类:
package com.jgw.supercode.ui.widget; import android.app.Activity; import android.content.Context; import android.text.Editable; import android.text.TextUtils; import android.text.TextWatcher; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import com.jgw.supercode.R; /** * Created by zx on 2016/6/3. */ public class SearchView extends FrameLayout { private EditText mEtSearch; private ImageView mIvClear; private TextView mTvClose; private OnTextChangeListener mOnTextChangeListener; public EditText getEtSearch() { return mEtSearch; } public void setEtSearch(EditText etSearch) { this.mEtSearch = etSearch; } public ImageView getIvClear() { return mIvClear; } public void setIvClear(ImageView ivClear) { this.mIvClear = ivClear; } public TextView getTvClose() { return mTvClose; } public void setTvClose(TextView tvClose) { this.mTvClose = tvClose; } public void setOnTextChangeListener(OnTextChangeListener onTextChangeListener) { this.mOnTextChangeListener = onTextChangeListener; } public SearchView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } private void init(Context context) { View rootView = LayoutInflater.from(context).inflate(R.layout.layout_search_edittext, null); mEtSearch = (EditText) rootView.findViewById(R.id.et_search); mIvClear = (ImageView) rootView.findViewById(R.id.iv_clear); mTvClose = (TextView) rootView.findViewById(R.id.tv_close); addView(rootView, new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); mIvClear.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mEtSearch.setText(""); } }); mTvClose.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ((Activity) getContext()).finish(); } }); mEtSearch.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { if (!TextUtils.isEmpty(s)) { mIvClear.setVisibility(View.VISIBLE); } else { mIvClear.setVisibility(View.GONE); } doTextChange(s); } }); } private void doTextChange(Editable s) { if (mOnTextChangeListener != null) { mOnTextChangeListener.onTextChange(s); } } public interface OnTextChangeListener { void onTextChange(Editable s); } }
2.Layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:gravity="center" android:background="@color/nav_background" android:layout_height="wrap_content"> <FrameLayout android:layout_gravity="center" android:layout_marginLeft="15dp" android:layout_marginTop="7dp" android:layout_marginBottom="7dp" android:layout_weight="1" android:layout_width="0dp" android:layout_height="30dp"> <EditText android:id="@+id/et_search" android:background="@drawable/shape_round_white_edit" android:hint="搜索" android:paddingLeft="@dimen/margin_small" android:textColor="@color/gray_32" android:drawablePadding="8dp" android:layout_gravity="left|center" android:drawableLeft="@mipmap/icon_search" android:textSize="13sp" android:layout_width="match_parent" android:layout_height="30dp" /> <ImageView android:id="@+id/iv_clear" android:paddingRight="@dimen/margin_small" android:src="@drawable/selector_search" android:layout_gravity="right|center" android:layout_width="wrap_content" android:layout_height="match_parent" /> </FrameLayout> <TextView android:id="@+id/tv_close" android:text="取消" android:textSize="13sp" android:paddingLeft="10dp" android:textColor="@android:color/white" android:paddingRight="15dp" android:gravity="center" android:layout_width="wrap_content" android:layout_height="match_parent" /> </LinearLayout>
3.背景
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="4dp"/> <solid android:color="@color/white"/> </shape>
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true" android:drawable="@mipmap/icon_edit_delete_pressed"></item> <item android:drawable="@mipmap/icon_edit_delete"></item> </selector>
使用:
svSearch.setOnTextChangeListener(new SearchView.OnTextChangeListener() { @Override public void onTextChange(Editable s) { keyWord = s.toString().trim(); requestData(1,s.toString().trim()); } });
相关文章推荐
- iOS多线程学习笔记
- IOS视图控制器左边缘右滑pop出栈
- iOS开发过程中字体的大小,加粗等问题的显示
- CodingNet - Learning - 6
- SULogger:iOS日志可视化工具
- 清理 iOS 项目不用的图片资源
- 清理 iOS 项目不用的图片资源
- iOS_如何画1px的线
- iOS开发3DTouch(二)
- iOS 基础篇11—ios应用数据存储方式(归档)
- iOS 基础篇10—ios应用数据存储方式(偏好设置)
- 写给iOS开发初学者
- [iOS]CocoaPods 卸载以及 安装旧版本 适用cocoapods>1.0.0
- 【iOS开发】初识runtime
- iOS开发3DTouch(一)
- 常见bug调试方法
- 【转】iOS实时卡顿监控
- iOS tableview 详解
- IOS学习笔记(六)inputAccessoryView,inputView
- IOS MD5