Android之搜索框的纯代码实现

更新时间:2023-07-01 16:49:03 阅读: 评论:0

Android之搜索框的纯代码实现
在Android开发中,搜索框是很常⽤的,但是控件中没有现成的,需要⾃⼰封装。那要怎么封装呢?
⽅式⼀:使⽤XML和JAVA代码相结合的⽅式。在XML中定义搜索的相关控件及布局,JAVA代码中进⾏相应事件的控制。⽅式⼆:对于浮动搜索框,可以使⽤SearchRecentSuggestionsProvider和archable来实现。
⽅式三:全部使⽤JAVA代码实现。
前⾯两种,⽹上的代码已经很多,这⾥使⽤⽅式三来实现。先来看看效果图。
功能:
(1)、搜索框中有提⽰。
(2)、输⼊内容后,提⽰⾃动清除,显⽰输⼊的内容,并在右边显⽰清空的图标。
(3)、点击搜索按钮后,将搜索结果输出。
依据这些功能,我们可以作如下分解。
(1)、输⼊框、清空图标、搜索按钮在同⼀⽔平线上,所以可以需要使⽤LinearLayout的⽔平布局来实现。
(2)、输⼊框可以使⽤EditText实现。
(3)、输⼊框的提⽰内容使⽤EditText的hint实现。
(4)、清空图标可以在EditText中绘制⼀个靠右的图标,并设定⼀定的感应区,以响应清空操作。
(5)、搜索按钮使⽤Button添加图⽚实现,同时添加点击事件的响应。
(6)、为了确保按钮外的空间被输⼊框占满,需要使⽤⽐重layout_weight=1来设置。
通过分解,⼤致可以理出需要⽤到的控件和相应的逻辑,下⾯是实现的代码。
SearchWidget.java
ample.archframetest;
t.Context;
t.res.Resources;
aphics.drawable.Drawable;
Editable;
InputType;
TextUtils;
TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class SearchWidget extends LinearLayout {
public final static int SEARCH_ID = 0x7ff20001;
public final static String HINT_NAME = "hint";
private EditText _data_editText = null;
private Button _arch_button = null;
private Context _context = null;
private Drawable _clear_drawable = null;
private Drawable _arch_drawable = null;
private Resources _res = null;
private AttributeSet _attrs = null;
private String _hint = "";
public SearchWidget(Context context, AttributeSet attrs) {
super(context, attrs);
if (context == null) {
return;
}
_context = context;
dispo
_attrs = attrs;
2013高考英语Init();
}
private void Init() {
InitParams();
InitAttrs();
InitControls();
InitLayout();
BindingEvents();
}
private void InitLayout() {
this.tOrientation(LinearLayout.HORIZONTAL);
}
private void InitParams() {
_res = _Resources();
_clear_drawable = _Drawable(R.drawable.clear);
_arch_drawable = _Drawable(R.drawable.arch);
}
private void InitAttrs() {
for (int i = 0; i < _AttributeCount(); i++) {cute
if (_AttributeName(i).equals(HINT_NAME)) {
_hint = _AttributeValue(i);
break;
}
}
}
private void InitControls() {
_data_editText = new EditText(_context);
_arch_button = new Button(_context);
LayoutParams dataLayoutParams = new LayoutParams(0,
LayoutParams.FILL_PARENT, 1);
_data_editText.tLayoutParams(dataLayoutParams);
_arch_button.tCompoundDrawablesWithIntrinsicBounds(null, null,    _arch_drawable, null);
_arch_button.tId(SEARCH_ID);
this.addView(_data_editText);
this.addView(_arch_button);
// addHint();
_data_editText.tHint(_hint);
// _data_editText.tFocusable(fal);
}
private void BindingEvents() {
_data_editText.addTextChangedListener(_arch_TextChanged);
_data_editText.tOnTouchListener(_arch_OnTouch);
}
public void tSearchOnClickListener(OnClickListener onclickListener) {
_arch_button.tOnClickListener(onclickListener);
}
public String getSearchData() {
return _Text().toString();
}
private TextWatcher _arch_TextChanged = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
Editable data = s;
if (TextUtils.isEmpty(data)) {
_data_editText.tCompoundDrawablesWithIntrinsicBounds(null,
null, null, null);
return;
}
_data_editText.tCompoundDrawablesWithIntrinsicBounds(null, null,    _clear_drawable, null);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// Log.e("TEST","3");
}
};
private OnTouchListener _arch_OnTouch = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (Action()) {
ca MotionEvent.ACTION_UP: {
int curX = (int) X();
String data = _Text().toString();
if (TextUtils.isEmpty(data)) {
return fal;
}
boolean isClearPosition = (curX > v.getWidth() - 88);
if (isClearPosition) {
Clear(event);
return true;
}
}
default: {
break;
}
}
return fal;
}
private void Clear(MotionEvent event) {explain的用法
int cacheInputType = _InputType();// backup
// the
// input
// type
_data_editText.tInputType(InputType.TYPE_NULL);// disable
/
/ soft
// input
_TouchEvent(event);// call native handler
_data_editText.tInputType(cacheInputType);// restore input
// type
// addHint();goji
_data_editText.tText("");
}
};
}
注:
(1)、给_arch_button定义⼀个id,以便响应点击事件。此处的SearchWidget.SEARCH_ID在实际中可能会与xml中定义的ID值有冲突,可以根据实际的情况作相应的调整。
(2)、定义HINT_NAME,以便在xml中调⽤搜索框控件时使⽤hint属性。
(3)、InitAttrs⽅法中,过虑出hint属性。
(4)、_data_editText.tHint(_hint)中设置输⼊框的内容提⽰。
(5)、BindingEvents添加_data_editText的⽂件改变和触摸事件的监听。
(6)、增加getSearchData函数供外部调⽤。
(7)、增加tSearchOnClickListener供外部设置搜索按钮的监听事件。
(8)、tCompoundDrawablesWithIntrinsicBounds动态修改输⼊框右侧的图标。
(9)、在_data_editText的layoutParams的布局参数设置中,将其宽度设置为0,⾼度设置为充满⽗容器,⽐重设置为1,以确保充满搜索按钮外的空间。
调⽤代码:
ample.archframetest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private SearchWidget _arch_widget = null;
lady codeprivate TextView _result_text=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
tContentView(R.layout.activity_main);
Init();
}
private void Init() {
FetchUIControls();
accordBindingEvents();
}
private void FetchUIControls() {
_arch_widget = (SearchWidget) findViewById(R.id.archWidget);  _result_text = (TextView) findViewById(sult);
}
freedom 女王private void BindingEvents() {
_arch_widget.tSearchOnClickListener(_clickListener);
}
private OnClickListener _clickListener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
ca SearchWidget.SEARCH_ID: {
Search();
break;
}
default: {
break;
weakest
公务员考试常识题}
}
}
};
protected void Search() {
String data=_SearchData();
_result_text.tText(data);
}
}
注:
(1)、使⽤常⽤的获取控件的⽅式来获取SearchWidget。

本文发布于:2023-07-01 16:49:03,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/163848.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:搜索   按钮   控件   事件
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图