2012-07-05 31 views
2

我有一個AutoCompleteTextView和我已經設置OnItemClick,但現在我想爲搜索按鈕設置OnKeyListener。我已經搜查,但沒有找到任何東西來幫助我。AutoCompleteTextView與OnKeyListener

這裏是我的自動完成XML:

<AutoCompleteTextView 
       android:id="@+id/autocomplete_stores" 
       android:layout_width="fill_parent" 
       android:layout_height="60dp" 
       android:layout_marginBottom="5dp" 
       android:hint="Stores Search:" 
       android:singleLine="true" 
       android:ellipsize="end" 
       android:imeOptions="actionSearch" /> 

和Java代碼:

AutoCompleteTextView searchStores; 
String[] searchStoresString; 
ArrayAdapter<String> searchStoresAdapter; 

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

    findviews(); 
    autocomplete(); 

    searchStores.setOnItemClickListener(this); 
} 

public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) 
{ 
    String str = (String) adapterView.getItemAtPosition(position); 
    Toast.makeText(this, str + " selected", Toast.LENGTH_SHORT).show(); 
} 

private void findviews() 
{ 
    searchStores = (AutoCompleteTextView) findViewById(R.id.autocomplete_stores); 
} 

private void autocomplete() 
{ 
    searchStoresString = getResources().getStringArray(R.array.stores_array); 
    searchStoresAdapter = new ArrayAdapter<String>(this, R.layout.list_item, searchStoresString); 

    searchStores.setThreshold(1); 
    searchStores.setAdapter(searchStoresAdapter); 
} 

一切工作正常。謝謝你的建議。

回答

0

嘗試,作爲對AutoCompleteTextView設置setKeyListener()

edtTitle = (AutoCompleteTextView) findViewById(R.id.edtTitle); 
     edtTitle.setOnKeyListener(new OnKeyListener() { 

      @Override 
      public boolean onKey(View arg0, int arg1, KeyEvent arg2) { 
       // TODO Auto-generated method stub 

       Toast.makeText(Current_Activity.this, arg1+"", 
       Toast.LENGTH_LONG).show(); 
       // return true; - if consumed 
       return false; 
      } 
     }); 
+0

做到這一點,或者你也可以使用setOnEditorActionListener爲AutoCompleteTextView獲取EditorInfo.IME_ACTION_SEARCH動作 –

+0

KeyListener和EditorListener有什麼區別? –

+0

@RotaryHeary:我認爲EditListener爲EditView,所以試試通過使用KeyListener –

0

您試試setKeyListener() ??更多help

+0

是,但得到的錯誤,已經tryed幾個方法。我只是不知道如何使用AutoCompleteTextView –