當前位置:首頁 » 安卓系統 » android自定義搜索框

android自定義搜索框

發布時間: 2025-10-13 11:57:31

1. 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去進行處理。

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

Android有自帶的一個控制項AutoCompleteTextView

具體用法如下:

java">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);
}
}

3. 怎麼使用android軟體的搜索功能

當你需要在你的應用程序中提供搜索服務時,通過使用Android的搜索框架,應用程序將顯示一個自定義搜索對話框來處理用戶的搜索請求。通過一個簡單的搜索按鈕或從您的應用程序中調用API,搜索對話框就會顯示在屏幕的頂部,並會自動顯示應用程序圖標。

本文將教你如何為你的應用程序提供一個自定義搜索對話框。這樣做,給您的用戶提供一個標准化的搜索體驗,並能增加如語音搜索和搜索建議等功能。

基礎知識

Android的搜索框架將代您管理的搜索對話框,您不需要自己去開發一個搜索框,不需要擔心要把搜索框放什麼位置,也不需要擔心搜索框影響您當前的界面。所有的這些工作都由SearchManager類來為您處理(以下簡稱「搜索管理器」),它管理的Android搜索對話框的整個生命周期,並執行您的應用程序將發送的搜索請求,返回相應的搜索關鍵字。

當用戶執行一個搜索,搜索管理器將使用一個專門的Intent把搜索查詢的關鍵字傳給您在配置文件中配置的處理搜索結果的Activity。從本質上講,所有你需要的就是一個Activity來接收Intent,然後執行搜索,並給出結果。具體來說,你需要的做的事就包括以下內容:

一個搜索配置
我們用個XML配置文件來對搜索對話框進行配置,包括一些功能的配置,如文本框,設置語音搜索和搜索建議中顯示的提示文字等。

一個用來處理搜索請求的Activity
這個Activity用來接收搜索查詢的內容,然後搜索您的數據並顯示搜索結果。

一種用戶執行搜索的途徑
默認情況下,一旦你配置了一個可搜索的Activity,設備搜索鍵(如果有)將調用搜索對話框。然而,你應該始終提供另一種手段,讓用戶可以調用搜索對話框,如在選項菜單中的搜索按鈕或其他用戶界面上的按鈕,因為不是所有的設備提供一個專門的搜索鍵。

創建一個搜索對話框配置文件

搜索框配置文件是一個用來配置您的應用程序中搜索框的設置的XML文件,這個文件一般命名為searchable.xml,並且必須保存在項目的res/xml/目錄下。

配置文件的根節點必須為,可以有一個或多個屬性。

熱點內容
資料庫優化面試題 發布:2025-10-13 14:16:46 瀏覽:758
超市密碼櫃如何操作 發布:2025-10-13 14:04:42 瀏覽:787
國外雲伺服器排行 發布:2025-10-13 14:02:22 瀏覽:884
zabbix源碼安裝 發布:2025-10-13 14:00:39 瀏覽:144
java微信群發 發布:2025-10-13 13:42:28 瀏覽:225
bat腳本賦值給變數賦值 發布:2025-10-13 13:35:00 瀏覽:63
加密的iv 發布:2025-10-13 13:30:28 瀏覽:887
windows文件夾只讀 發布:2025-10-13 13:30:23 瀏覽:464
在線看片mp4ftp 發布:2025-10-13 13:29:41 瀏覽:880
加密詞 發布:2025-10-13 13:28:54 瀏覽:209