【问题标题】:AutoCompleteTextView with OnKeyListener带有 OnKeyListener 的 AutoCompleteTextView
【发布时间】:2012-07-05 13:35:53
【问题描述】:

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

一切正常。感谢您的建议。

【问题讨论】:

    标签: java android autocomplete textview keylistener


    【解决方案1】:

    你试试setKeyListener() ??更多help

    【讨论】:

    • 是的,但出现错误,尝试了几种方法。我只是不知道如何使用 AutoCompleteTextView 来做到这一点
    【解决方案2】:

    尝试为 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;
                }
            });
    

    【讨论】:

    • 或者您也可以使用 setOnEditorActionListener for AutoCompleteTextView 来获取 EditorInfo.IME_ACTION_SEARCH 操作
    • KeyListener和EditorListener有什么区别?
    • @RotaryHeary :我认为 EditorListener i for EditView 所以尝试使用 KeyListener
    • 这里不工作。出于某种原因,听众永远不会开火。顺便说一句,你有一个(括号)语法错误。
    • 是的,这些事件监听器都不会触发。我不明白为什么 OP 将此解决方案标记为答案。
    猜你喜欢
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    相关资源
    最近更新 更多