7

我想構建一個包含sherlock動作欄和其中的搜索視圖的應用程序。不過,我希望這個searchview具有像autocompleteTextView一樣的自動完成功能。有沒有任何直接的實現來單獨使用searchview?或者我應該使用操作欄內的autocompleteTextView來做到這一點?我在那裏找到了幾個帖子,但他們都沒有幫助我。這些帖子只討論創建autocompleteTextView,但我希望搜索視圖具有此功能。任何想法如何完成?Android - 帶有自動完成功能的searchview在動作欄內

回答

17

爲此,我有AutoCompleteTextView創建一個佈局和ActionBar其呼叫Custom layout in ActionBar.

添加它之後,我有android.R.layout.simple_dropdown_item_1line創建適配器。將其設置爲AutoCompleteTextView

檢查下面的代碼:

enter image description here

package com.example.testapp; 

import android.annotation.TargetApi; 
import android.app.ActionBar; 
import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.AutoCompleteTextView; 

@TargetApi(11) 
public class MainActivity extends Activity { 

    private static final String[] COUNTRIES = new String[] { "Belgium", 
      "France", "France_", "Italy", "Germany", "Spain" }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     ActionBar actionBar = getActionBar(); 
     actionBar.setDisplayHomeAsUpEnabled(true); 
     actionBar.setDisplayShowCustomEnabled(true); 
     // actionBar.setDisplayShowTitleEnabled(false); 
     // actionBar.setIcon(R.drawable.ic_action_search); 

     LayoutInflater inflator = (LayoutInflater) this 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View v = inflator.inflate(R.layout.actionbar, null); 

     actionBar.setCustomView(v); 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_dropdown_item_1line, COUNTRIES); 
     AutoCompleteTextView textView = (AutoCompleteTextView) v 
       .findViewById(R.id.editText1); 
     textView.setAdapter(adapter); 

    } 

} 

佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Action Bar:" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="#FFFFFF" /> 

    <AutoCompleteTextView 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:imeOptions="actionSearch" 
     android:inputType="textAutoComplete|textAutoCorrect" 
     android:textColor="#FFFFFF" > 

     <requestFocus /> 
    </AutoCompleteTextView> 

</LinearLayout> 

有關詳情,請這個快速舉報通道:onetwothree

祝你好運!

+0

多德的完整代碼,請添加一些解釋 – 2013-04-05 05:49:34

+1

@MoJo:做.... – 2013-04-05 05:56:06

+0

+1做實至名歸:)從我 – 2013-04-05 05:56:47

1

我有一個簡單的方法來做到這一點,而不使用自動完成textview,只是把一個簡單的列表適配器,其中包含項目的ArrayList(itemArrayList),建議與搜索view.Set適配器到您的SearchAutoComplete和自動完成功能將在下面工作是我的code.With,你不必使用額外的佈局。

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
// Inflate the menu; this adds items to the action bar if it is present. 
getMenuInflater().inflate(R.menu.main, menu); 

MenuItem searchItem = menu.findItem(R.id.search); 
SearchView mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem); 

SearchAutoComplete searchAutoComplete = (SearchAutoComplete)  mSearchView.findViewById(android.support.v7.appcompat.R.id.search_src_text); 
searchAutoComplete.setTextColor(Color.WHITE); 

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
android.R.layout.simple_dropdown_item_1line, itemArrayList); 
searchAutoComplete.setAdapter(adapter); 

SearchManager searchManager = 
(SearchManager) getSystemService(this.SEARCH_SERVICE); 
mSearchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName())); 
return true; 
} 

點擊該項目應出現在搜索查看推薦的項目,以便添加以下代碼只是後您將適配器連接到onCreateOptionmenu(...)方法中searchAutoComplete。

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, 
long id) { 
// TODO Auto-generated method stub 

String searchString=(String)parent.getItemAtPosition(position); 
searchAutoComplete.setText(""+searchString); 
Toast.makeText(MainActivity.this, "you clicked "+searchString, Toast.LENGTH_LONG).show(); 

} 
}); 

你也可以看到在這個Link