我有一个AutoCompleteTextView
并且我已经设置了OnItemClick
,但现在我想设置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);
}
一切正常。谢谢指教。