android實(shí)現(xiàn)搜索功能并將搜索結(jié)果保存到SQLite中(實(shí)例代碼)
運(yùn)行結(jié)果:



涉及要點(diǎn):
- ListView+EditText+ScrollView實(shí)現(xiàn)搜索效果顯示
- 監(jiān)聽(tīng)軟鍵盤(pán)回車(chē)執(zhí)行搜索
- 使用TextWatcher( )實(shí)時(shí)篩選
- 將搜索內(nèi)容存儲(chǔ)到SQLite中(可清空歷史記錄)
- 監(jiān)聽(tīng)EditText的焦點(diǎn),獲得焦點(diǎn)彈出軟鍵盤(pán)同時(shí)顯示搜索歷史,失去焦點(diǎn)隱藏軟件盤(pán)和ListView。
實(shí)現(xiàn)過(guò)程比較簡(jiǎn)單,都是常用的,這里就不講解了。代碼可直接復(fù)制使用。
實(shí)現(xiàn)過(guò)程:
MainActivity.java
public class MainActivity extends Activity {
private EditText et_search;
private TextView tv_tip;
private MyListView listView;
private TextView tv_clear;
ScrollView scrollView;
private RecordSQLiteOpenHelper helper = new RecordSQLiteOpenHelper(this);
private SQLiteDatabase db;
private BaseAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView(); // 初始化控件
// 清空搜索歷史
tv_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteData();
queryData("");
}
});
et_search.setOnKeyListener(new View.OnKeyListener() {// 輸入完后按鍵盤(pán)上的搜索鍵
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {// 修改回車(chē)鍵功能
// 隱藏鍵盤(pán)
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
// 按完搜索鍵后將當(dāng)前查詢(xún)的關(guān)鍵字保存起來(lái),如果該關(guān)鍵字已經(jīng)存在就不執(zhí)行保存
boolean hasData = hasData(et_search.getText().toString().trim());
if (!hasData) {
insertData(et_search.getText().toString().trim());
queryData("");
}
Toast.makeText(MainActivity.this, "點(diǎn)擊軟鍵盤(pán)搜索!", Toast.LENGTH_SHORT).show();
}
return false;
}
});
et_search.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if (b) { //獲得
scrollView.setVisibility(View.VISIBLE);
} else {//市區(qū)焦點(diǎn)
scrollView.setVisibility(View.GONE);
}
}
});
// 搜索框的文本變化實(shí)時(shí)監(jiān)聽(tīng)
et_search.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 (s.toString().trim().length() == 0) {
tv_tip.setText("搜索歷史");
} else {
tv_tip.setText("搜索結(jié)果");
}
String tempName = et_search.getText().toString();
// 根據(jù)tempName去模糊查詢(xún)數(shù)據(jù)庫(kù)中有沒(méi)有數(shù)據(jù)
queryData(tempName);
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textView = (TextView) view.findViewById(android.R.id.text1);
String name = textView.getText().toString();
et_search.setText(name);
Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); //隱藏軟鍵盤(pán)
et_search.clearFocus();
et_search.setText("");
}
});
// 插入測(cè)試數(shù)據(jù)
Date date = new Date();
long time = date.getTime();
insertData("LY" + time);
queryData(""); // 第一次進(jìn)入查詢(xún)所有的歷史記錄
}
/**
* 插入數(shù)據(jù)
*/
private void insertData(String tempName) {
db = helper.getWritableDatabase();
db.execSQL("insert into records(name) values('" + tempName + "')");
db.close();
}
/**
* 模糊查詢(xún)數(shù)據(jù)
*/
private void queryData(String tempName) {
Cursor cursor = helper.getReadableDatabase().rawQuery(
"select id as _id,name from records where name like '%" + tempName + "%' order by id desc ", null);
// 創(chuàng)建adapter適配器對(duì)象
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, new String[]{"name"},
new int[]{android.R.id.text1}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
// 設(shè)置適配器
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
/**
* 檢查數(shù)據(jù)庫(kù)中是否已經(jīng)有該條記錄
*/
private boolean hasData(String tempName) {
Cursor cursor = helper.getReadableDatabase().rawQuery(
"select id as _id,name from records where name =?", new String[]{tempName});
//判斷是否有下一個(gè)
return cursor.moveToNext();
}
/**
* 清空數(shù)據(jù)
*/
private void deleteData() {
db = helper.getWritableDatabase();
db.execSQL("delete from records");
db.close();
}
private void initView() {
et_search = (EditText) findViewById(R.id.et_search);
scrollView = findViewById(R.id.showSearch);
tv_tip = (TextView) findViewById(R.id.tv_tip);
listView = (com.cwvs.microlife.MyListView) findViewById(R.id.listView);
tv_clear = (TextView) findViewById(R.id.tv_clear);
// 調(diào)整EditText左邊的搜索按鈕的大小
Drawable drawable = getResources().getDrawable(R.drawable.search);
drawable.setBounds(0, 0, 60, 60);// 第一0是距左邊距離,第二0是距上邊距離,60分別是長(zhǎng)寬
et_search.setCompoundDrawables(drawable, null, null, null);// 只放左邊
}
}
RecordSQLiteOpenHelper.java
public class RecordSQLiteOpenHelper extends SQLiteOpenHelper {
private static String name = "temp.db";
private static Integer version = 1;
public RecordSQLiteOpenHelper(Context context) {
super(context, name, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table records(id integer primary key autoincrement,name varchar(200))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
MyListView.java
public class MyListView extends ListView {
public MyListView(Context context) {
super(context);
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:focusable="true" android:focusableInTouchMode="true" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="fill_parent" android:layout_height="50dp" android:background="#be9999" android:orientation="horizontal" android:paddingRight="16dp"> <ImageView android:layout_width="45dp" android:layout_height="45dp" android:layout_gravity="center_vertical" android:padding="10dp" android:src="@drawable/back" /> <EditText android:id="@+id/et_search" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="264" android:background="@null" android:drawableLeft="@drawable/search" android:drawablePadding="8dp" android:gravity="start|center_vertical" android:hint="輸入查詢(xún)的關(guān)鍵字" android:imeOptions="actionSearch" android:singleLine="true" android:textColor="@android:color/white" android:textSize="16sp" /> </LinearLayout> <ScrollView android:id="@+id/showSearch" android:layout_width="wrap_content" android:layout_height="300dp" android:visibility="gone"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingLeft="20dp"> <TextView android:id="@+id/tv_tip" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="left|center_vertical" android:text="搜索歷史" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#EEEEEE" /> <liyue.edu.cn.ncst.app.MyListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#EEEEEE" /> <TextView android:id="@+id/tv_clear" android:layout_width="match_parent" android:layout_height="40dp" android:background="#F6F6F6" android:gravity="center" android:text="清除搜索歷史" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginBottom="20dp" android:background="#EEEEEE" /> </LinearLayout> </ScrollView> </LinearLayout>
到此這篇關(guān)于android實(shí)現(xiàn)搜索功能并將搜索結(jié)果保存到SQLite中(實(shí)例代碼)的文章就介紹到這了,更多相關(guān)android 搜索功能搜索結(jié)果保存sqlite內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android自定義View實(shí)現(xiàn)搜索框(SearchView)功能
- Android開(kāi)發(fā)實(shí)現(xiàn)仿京東商品搜索選項(xiàng)卡彈窗功能
- Android 高德地圖之poi搜索功能的實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)ListView的A-Z字母排序和過(guò)濾搜索功能 實(shí)現(xiàn)漢字轉(zhuǎn)成拼音
- Android搜索框(SearchView)的功能和用法詳解
- Android流式布局實(shí)現(xiàn)歷史搜索記錄功能
- Android ListView用EditText實(shí)現(xiàn)搜索功能效果
- Android實(shí)現(xiàn)搜索功能并本地保存搜索歷史記錄
- Android編程之SMS讀取短信并保存到SQLite的方法
- android創(chuàng)建數(shù)據(jù)庫(kù)(SQLite)保存圖片示例
相關(guān)文章
Android應(yīng)用實(shí)現(xiàn)點(diǎn)擊按鈕震動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android應(yīng)用實(shí)現(xiàn)點(diǎn)擊按鈕震動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
android實(shí)現(xiàn)字體閃爍動(dòng)畫(huà)的方法
這篇文章主要介紹了android實(shí)現(xiàn)字體閃爍動(dòng)畫(huà)的方法,涉及Android中線程調(diào)用和Timer計(jì)時(shí)器的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
Android使用NumberPicker實(shí)現(xiàn)滑輪日期選擇器
這篇文章主要為大家介紹了如何使用Android中的NumberPicker控件,以一種簡(jiǎn)單而直觀的方式實(shí)現(xiàn)滑輪式的日期選擇器,需要的小伙伴可以參考一下2023-06-06
Android實(shí)現(xiàn)直接播放麥克風(fēng)采集到的聲音
這篇文章主要介紹了Android實(shí)現(xiàn)直接播放麥克風(fēng)采集到的聲音,涉及Android音頻操作的相關(guān)技巧,需要的朋友可以參考下2015-06-06
Android開(kāi)發(fā)之Wifi基礎(chǔ)教程
這篇文章主要介紹了Android開(kāi)發(fā)Wifi基礎(chǔ)教程,實(shí)例分析了Wifi的各種常見(jiàn)基本技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02
Android原生實(shí)現(xiàn)多線程斷點(diǎn)下載實(shí)例代碼
本篇文章主要介紹了Android原生實(shí)現(xiàn)多線程斷點(diǎn)下載實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
Android編程實(shí)現(xiàn)開(kāi)始及停止service的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)開(kāi)始及停止service的方法,涉及Android針對(duì)service的開(kāi)始、停止、綁定等操作相關(guān)技巧與注意事項(xiàng),需要的朋友可以參考下2016-01-01

