android 实现 搜索保存历史记录功能
大家应该做这个功能 首先想到的是用AutoCompleteTextView 这个控件非常好用 ,而且代码写起来也是非常简单 而且还是自动匹配提示
具体实现方法:
代码实现:
-
<AutoCompleteTextView
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:id="@+id/autotext"
-
/>
-
-
-
public class MainActivity extends Activity {
-
private AutoCompleteTextView autotext;
-
private ArrayAdapter<String> arrayAdapter;
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.test);
-
-
-
autotext =(AutoCompleteTextView) findViewById(R.id.autotext);
-
String [] arr={"aa","aab","aac"};
-
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arr);
-
autotext.setAdapter(arrayAdapter);
-
}
-
-
}
这样就可以实现简单的 搜索历史匹配 我们要做的 其实就是缓存输入的内容到 本地 下面就是实现保存 搜索内容到本地 和 清空本地历史的 方法
//保存搜索内容到本地
-
<pre name="code" class="java">public void save() {
-
String text = mKeywordEt.getText().toString();
-
String oldText = mSharePreference.getString(SEARCH_HISTORY, "");
-
StringBuilder builder = new StringBuilder(text);
-
builder.append("," + oldText);
-
if (!TextUtils.isEmpty(text) && !oldText.contains(text + ",")) {
-
SharedPreferences.Editor myEditor = mSharePreference.edit();
-
myEditor.putString(SEARCH_HISTORY, builder.toString());
-
myEditor.commit();
-
}
-
updateData();
-
}
-
-
-
//清空本地历史
-
public void cleanHistory() {
-
SharedPreferences.Editor editor = mSharePreference.edit();
-
editor.clear();
-
editor.commit();
-
updateData();
-
mSearchHistoryLl.setVisibility(View.GONE);
-
SingleToast.show(this, getString(R.string.clear_history_success), Toast.LENGTH_SHORT);
-
}
Ps:AutoCompleteTextView 还有一个小问题 默认是从第二个字符开始匹配的
如果设置输入第一个字符就进行提示 加入下面这行代码autotext.setThreshold(1);
上面大概介绍了一下AutoCompleteTextView这个控件。但是我们的项目要做的是 显示出所有搜索的历史记录 效果图是这样的
想了一下 用 AutoCompleteTextView 不太合适 决定自己用listView 实现 话不多说 直接上代码
下拉弹出layout布局
-
<LinearLayout
-
android:id="@+id/search_history_ll"
-
android:orientation="vertical"
-
android:layout_width="match_parent"
-
android:layout_below="@id/global_search_action_bar_rl"
-
android:layout_height="wrap_content">
-
<TextView
-
android:id="@+id/contentTextView"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:textSize="@dimen/text_size_title_h2"
-
android:text="@string/search_history"
-
android:paddingLeft="10dp"
-
android:textColor="@color/text_gray"/>
-
<ListView
-
android:id="@+id/search_history_lv"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:cacheColorHint="@android:color/transparent"
-
android:listSelector="@drawable/list_item_selector">
-
</ListView>
-
<Button
-
android:id="@+id/clear_history_btn"
-
android:layout_width="210dp"
-
android:layout_height="@dimen/button_common_height"
-
android:layout_below="@id/rise_crash_ll"
-
android:layout_marginTop="5dp"
-
android:textColor="@color/text_btn_selector"
-
android:layout_gravity="center"
-
android:textSize="@dimen/text_size_title_h2"
-
android:layout_centerHorizontal="true"
-
android:text="@string/clear_search_history"
-
android:background="@drawable/round_btn_selector"
-
style="?android:buttonBarButtonStyle"/>
-
</LinearLayout>
-
-
-
-
-
activity 的代码
-
-
-
import android.content.SharedPreferences;
-
import android.os.Bundle;
-
import android.text.Editable;
-
import android.text.TextUtils;
-
import android.text.TextWatcher;
-
import android.view.View;
-
import android.widget.AdapterView;
-
import android.widget.ArrayAdapter;
-
import android.widget.EditText;
-
import android.widget.ImageView;
-
import android.widget.LinearLayout;
-
import android.widget.ListView;
-
import android.widget.TextView;
-
import android.widget.Toast;
-
-
-
import com.ccvideo.R;
-
import com.yizhibo.video.adapter.SearchAdapter;
-
import com.yizhibo.video.app.YZBApplication;
-
import com.yizhibo.video.base.BaseListActivity;
-
import com.yizhibo.video.utils.Constants;
-
import com.yizhibo.video.utils.SingleToast;
-
import com.yizhibo.video.utils.Utils;
-
-
-
public class SearchListActivity extends BaseListActivity implements View.OnClickListener {
-
public static final String EXTRA_KEY_TYPE = "extra_key_type";
-
private static final String PRE_SEARCH_HISTORY = "pre_search_history";
-
private static final String SEARCH_HISTORY = "search_history";
-
-
-
private EditText mKeywordEt;
-
private TextView mOperationTv;
-
private ArrayAdapter<String> mArrAdapter;
-
private SharedPreferences mSharePreference;
-
-
-
private LinearLayout mSearchHistoryLl;
-
private List<String> mHistoryKeywords;
-
private ListView mListView;
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
mSharePreference = YZBApplication.getApp().getSharedPreferences(PRE_SEARCH_HISTORY, 0);
-
setContentView(R.layout.activity_search_list);
-
mKeywordEt = (EditText) findViewById(R.id.tab_bar_keyword_et);
-
mHistoryKeywords = new ArrayList<String>();
-
-
mKeywordEt.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) {
-
if (s.length() == 0) {
-
mAdapter.clear();
-
mAdapter.notifyDataSetChanged();
-
mOperationTv.setText(R.string.cancel);
-
mEmptyView.hide();
-
clearKeywordIv.setVisibility(View.GONE);
-
if (mHistoryKeywords.size() > 0) {
-
mSearchHistoryLl.setVisibility(View.VISIBLE);
-
} else {
-
mSearchHistoryLl.setVisibility(View.GONE);
-
}
-
} else {
-
mSearchHistoryLl.setVisibility(View.GONE);
-
mOperationTv.setText(R.string.search);
-
clearKeywordIv.setVisibility(View.VISIBLE);
-
}
-
}
-
-
-
@Override
-
public void afterTextChanged(Editable s) {
-
-
-
}
-
});
-
mKeywordEt.requestFocus();
-
mOperationTv = (TextView) findViewById(R.id.tab_bar_cancel_tv);
-
mOperationTv.setOnClickListener(new View.OnClickListener() {
-
@Override
-
public void onClick(View v) {
-
if (mKeywordEt.getText().length() > 0) {
-
hideInputMethod();
-
save();
-
} else {
-
finish();
-
}
-
}
-
});
-
initSearchHistory();
-
-
}
-
-
public void initSearchHistory() {
-
mSearchHistoryLl = (LinearLayout) findViewById(R.id.search_history_ll);
-
ListView listView = (ListView) findViewById(R.id.search_history_lv);
-
findViewById(R.id.clear_history_btn).setOnClickListener(this);
-
String history = mPref.getString(Preferences.KEY_SEARCH_HISTORY_KEYWORD);
-
if (!TextUtils.isEmpty(history)){
-
List<String> list = new ArrayList<String>();
-
for(Object o : history.split(",")) {
-
list.add((String)o);
-
}
-
mHistoryKeywords = list;
-
}
-
if (mHistoryKeywords.size() > 0) {
-
mSearchHistoryLl.setVisibility(View.VISIBLE);
-
} else {
-
mSearchHistoryLl.setVisibility(View.GONE);
-
}
-
mArrAdapter = new ArrayAdapter<String>(this, R.layout.item_search_history, mHistoryKeywords);
-
listView.setAdapter(mArrAdapter);
-
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
-
@Override
-
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
-
mKeywordEt.setText(mHistoryKeywords.get(i));
-
mSearchHistoryLl.setVisibility(View.GONE);
-
}
-
});
-
mArrAdapter.notifyDataSetChanged();
-
}
-
-
public void save() {
-
String text = mKeywordEt.getText().toString();
-
String oldText = mPref.getString(Preferences.KEY_SEARCH_HISTORY_KEYWORD);
-
if (!TextUtils.isEmpty(text) && !oldText.contains(text)) {
-
mPref.putString(Preferences.KEY_SEARCH_HISTORY_KEYWORD, text + "," + oldText);
-
mHistoryKeywords.add(0,text);
-
}
-
mArrAdapter.notifyDataSetChanged();
-
}
-
-
-
public void cleanHistory() {
-
mPref.remove(Preferences.KEY_SEARCH_HISTORY_KEYWORD);
-
mHistoryKeywords.clear();
-
mArrAdapter.notifyDataSetChanged();
-
mSearchHistoryLl.setVisibility(View.GONE);
-
SingleToast.show(this, getString(R.string.clear_history_success), Toast.LENGTH_SHORT);
-
}
-
public void updateData(){
-
String history = mSharePreference.getString(SEARCH_HISTORY, "");
-
mHistoryArr = history.split(",");
-
mArrAdapter = new ArrayAdapter<String>(this,
-
R.layout.activity_searchhistory, mHistoryArr);
-
mListView.setAdapter(mArrAdapter);
-
mArrAdapter.notifyDataSetChanged();
-
}
-
-
-
@Override
-
public void onClick(View view) {
-
switch (view.getId()) {
-
case R.id.clear_history_btn:
-
cleanHistory();
-
break;
-
}
-
}
-
}
之前的这个demo里面少了share commit的操作,导致会有搜索记录没有去重的问题,已经更新。附上新的源码下载地址 源码demo ,同时谢谢ysc123shift同学的善意提醒!做技术互相帮助和分享很重要。谢谢!
效果图就是上面的图片。希望能帮到大家。 欢迎大家加入我的qq 群:454430053。
文章来源: wukong.blog.csdn.net,作者:再见孙悟空_,版权归原作者所有,如需转载,请联系作者。
原文链接:wukong.blog.csdn.net/article/details/50427194
- 点赞
- 收藏
- 关注作者
评论(0)