導航:首頁 > 編程語言 > android搜索功能代碼

android搜索功能代碼

發布時間:2025-07-02 18:57:50

Ⅰ android應用中的搜索功能怎麼實現的

在APP應用中啟用搜索
在app應用中,至少要執行如下的三個步驟,才能讓app應用能夠進行檢索。如果要提供搜索建議,還需要執行第4步:
編寫搜索配置的XML文件
編寫搜索的activity類
在Android的manifest.xml文件中,對兩面兩個步驟的工作進行配置。
如果要使用搜索建議,則需要增加一個contentprovider。
配置搜索的XML配置文件
首先看下如何配置搜索的XML配置文件。先命名配置文件名稱為searchable.xml,保存在res/xml文件夾中。然後需要設置搜索框的文本,並且應該增加一個hint的提示文本信息,如下代碼所示:
<searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/search_label"> android:hint="@string/search_hint" </searchable>

關於搜索配置文件有很多的配置選項,建議參考Android的手冊可以獲得更多:
http://developer.android.com/guide/topics/search/searchable-config.html。
增加搜索的Activity
當用戶進行搜索時,Android調用activity進行搜索,代碼如下:
publicclass SampleSearchActivity extends ListActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); handleIntent(getIntent()); }public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); handleIntent(getIntent()); } public void onNewIntent(Intent intent) { setIntent(intent); handleIntent(intent); } public void onListItemClick(ListView l, View v, int position, long id) { // 點每個搜索結果時的處理代碼 } private void handleIntent(Intent intent) { if (Intent.ACTION_SEARCH.equals(intent.getAction())) { String query = intent.getStringExtra(SearchManager.QUERY); doSearch(query); } } private void doSearch(String queryStr) { //執行真正的查詢結果處理 } }

在上面的代碼中,在handleIntent方法中,當按下搜索按鈕,系統就會自動發送Intent,action是Intent.ACTION_SEARCH,然後通過intent.getStringExtra(SearchManager.QUERY);獲得要搜索的字元串。
其中為什麼要包含onNewIntent()方法呢?主要是因為Android的back後退機制。Android會默認把每一個新的activity放到activity棧的頂部。如果用戶點了後退鍵,則會關閉棧頂部的activity。嘗試考慮一種情況,用戶搜索一個內容並且系統列出了結果,如果用戶發現結果不是他所要的,或者希望重新檢索,則會重新點擊搜索按鍵,這樣將會產生一個新的搜索activity的實例,在activity棧中就會有兩個搜索的activity,這是開發者並不期待的,所以,需要將這個搜索的activity聲明為singleTop類型的activity,這樣的話,無論用戶按返回鍵還是盡心個多次的搜索,在acitivty棧中始終保持的是一個搜索activity的實例。因為當activity被設置為singleTop的載入模式時,如果堆棧的頂部已經存在了該Activity,那麼,它便不會重新創建,而是調用onNewIntent。如果,該Activity存在,但不是在頂部,那麼該Activity依然要重新創建。
mainifest配置文件
接下來,需要對manifest配置文件進行配置,必須要對其中進行如下配置:
搜索的activity.
使用搜索的intent
activity啟動模式
searchable.xml中的元數據
更多的定義搜索的元數據
下面是典型的一個搜索的配置
<application android:icon="@drawable/icon" android:label="@string/app_name" android:name=".YourApp" > <meta-data android:name="android.app.default_searchable" android:value=".YourSearchActivity" /> <activity android:label="@string/app_name" android:launchMode="singleTop" android:name=".YourSearchActivity" > <intent-filter > <action android:name="android.intent.action.SEARCH" /> </intent-filter> <intent-filter > <action android:name="android.intent.action.VIEW" /> </intent-filter> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" /> </activity> </application>

在上面的典型配置中,要注意如下幾點:
1)由於當調用搜索activity時,Android調用的是android.intent.action.SEARCH作為搜索的intent,所以必須在intent-filter中包含android.intent.action.SEARCH。
2)在<meta-data>中,指出了searchable.xml的位置
3)同樣在<meta-data>中,通過:
<meta-data android:name="android.app.default_searchable" android:value=".YourSearchActivity" />

指出了當執行搜索的字元串提交時,將調用哪一個activity去進行處理。

Ⅱ Android 系統搜索框 如何限制輸入字數長度

android 搜索框就是一個EditText輸入控制項,或者是EditText的子類

長度限制方式有以下幾種:

方法一:

在 xml 文件中設置文本編輯框屬性作字元數限制

如:android:maxLength="10" 即限制最大輸入字元個數為10


方法二:

在代碼中使用InputFilter 進行過濾

//editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)}); 即限定最大輸入字元數為20

示例代碼如下:

java">{
/**.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

EditTexteditText=(EditText)findViewById(R.id.entry);
editText.setFilters(newInputFilter[]{newInputFilter.LengthFilter(20)});
}
}

方法三:

利用 TextWatcher 進行限制,TextWatcher是注冊一個內存輸入的改變事件,當你的輸入框輸入字元和刪除字元都會觸發

實現代碼如下:

packagecie.textEdit;

importandroid.text.Editable;
importandroid.text.Selection;
importandroid.text.TextWatcher;
importandroid.widget.EditText;

/*
*監聽輸入內容是否超出最大長度,並設置游標位置
**/
{

privateintmaxLen=0;
privateEditTexteditText=null;


publicMaxLengthWatcher(intmaxLen,EditTexteditText){
this.maxLen=maxLen;
this.editText=editText;
}

publicvoidafterTextChanged(Editablearg0){
//TODOAuto-generatedmethodstub

}

publicvoidbeforeTextChanged(CharSequencearg0,intarg1,intarg2,
intarg3){
//TODOAuto-generatedmethodstub

}

publicvoidonTextChanged(CharSequencearg0,intarg1,intarg2,intarg3){
//TODOAuto-generatedmethodstub
Editableeditable=editText.getText();
intlen=editable.length();

if(len>maxLen)
{
intselEndIndex=Selection.getSelectionEnd(editable);
Stringstr=editable.toString();
//截取新字元串
StringnewStr=str.substring(0,maxLen);
editText.setText(newStr);
editable=editText.getText();

//新字元串的長度
intnewLen=editable.length();
//舊游標位置超過字元串長度
if(selEndIndex>newLen)
{
selEndIndex=editable.length();
}
//設置新游標所在的位置
Selection.setSelection(editable,selEndIndex);

}
}

}

有關EditText 即Android輸入框的更多用法,建議查看官網API文檔

Ⅲ Android中如何讓一個EditView被點擊後出現搜索框,搜索框已經實現

Android有自帶的一個控制項AutoCompleteTextView

具體用法如下:

main.xml代碼如下:

<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionHint="請選擇你喜歡的歌曲"
android:completionThreshold="1"
android:dropDownHorizontalOffset="20dp"
android:ems="10"
android:text="AutoCompleteTextView">

<requestFocus/>
</AutoCompleteTextView>

</LinearLayout>

java代碼為:

importandroid.app.Activity;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;
importandroid.graphics.drawable.BitmapDrawable;
importandroid.os.Bundle;
importandroid.view.MotionEvent;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.view.View.OnTouchListener;
importandroid.widget.ArrayAdapter;
importandroid.widget.AutoCompleteTextView;
importandroid.widget.Button;
importandroid.widget.ImageView;

{

//定義字元串數組作為提示的文本
String[]books=newString[]{"rollen","rollenholt","rollenren","roll"};

@Override
protectedvoidonCreate(BundlesavedInstanceState){
//TODOAuto-generatedmethodstub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//創建一個ArrayAdapter封裝數組
ArrayAdapter<String>av=newArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line,books);
AutoCompleteTextViewauto=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
auto.setAdapter(av);
}
}
閱讀全文

與android搜索功能代碼相關的資料

熱點內容
ug裝配體找不到文件部件已刪除 瀏覽:629
小網站怎麼弄出來 瀏覽:649
jsp表單加參數 瀏覽:607
蘋果5s手機老是卡屏 瀏覽:58
js給php變數賦值 瀏覽:446
雜志版本號是什麼意思 瀏覽:223
地圖特效代碼 瀏覽:192
去除思科配置文件中的號 瀏覽:196
運行的16位程序太多 瀏覽:1
蘋果mac用什麼軟體好學編程 瀏覽:681
ai中線段怎麼添加寬度配置文件 瀏覽:956
lol文件怎麼找不到game 瀏覽:142
aecc視頻教程 瀏覽:983
linux怎麼查看資料庫用戶名 瀏覽:182
cefs文件系統 瀏覽:404
學平面設計個編程哪個好 瀏覽:701
如何把編程文件轉為hex 瀏覽:80
清除蘋果地圖來自地址 瀏覽:233
已經打開的文件如何清理 瀏覽:685
視頻網站有什麼用 瀏覽:70

友情鏈接