使用OnKeyListener的AutoCompleteTextView

时间:2012-07-05 07:10:54

标签: java android autocomplete textview keylistener

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

一切都很好。谢谢你的建议。

2 个答案:

答案 0 :(得分:0)

你尝试setKeyListener() ??更多help

答案 1 :(得分: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;
            }
        });