2

我有一个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);
}

一切正常。谢谢指教。

4

2 回答 2

0

你试试setKeyListener()??寻求更多帮助

于 2012-07-05T07:14:30.583 回答
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;
            }
        });
于 2012-07-05T07:26:49.727 回答