2012-07-05 29 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 –