簡體   English   中英

具有OnKeyListener的AutoCompleteTextView

[英]AutoCompleteTextView with OnKeyListener

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

一切正常。 感謝您的建議。

您嘗試setKeyListener() 尋求更多幫助

嘗試為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;
            }
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM