Java 带有OnKeyListener的AutoCompleteTextView
我有一个Java 带有OnKeyListener的AutoCompleteTextView,java,android,autocomplete,textview,keylistener,Java,Android,Autocomplete,Textview,Keylistener,我有一个AutoCompleteTextView,我已经设置了onimclick,但是现在我想为搜索按钮设置onkeylister。我已经找过了,但没有找到任何帮助我的东西 以下是我的自动完成xml: <AutoCompleteTextView android:id="@+id/autocomplete_stores" android:layout_width="fill_parent"
AutoCompleteTextView
,我已经设置了onimclick
,但是现在我想为搜索按钮设置onkeylister
。我已经找过了,但没有找到任何帮助我的东西
以下是我的自动完成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);
}
AutoCompleteTextView searchStores;
字符串[]searchStoresString;
阵列适配器搜索存储适配器;
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.stores);
FindView();
自动完成();
searchStores.setOnItemClickListener(this);
}
public void onItemClick(AdapterView AdapterView,视图视图,整型位置,长id)
{
String str=(字符串)adapterView.getItemAtPosition(位置);
Toast.makeText(this,str+“selected”,Toast.LENGTH_SHORT).show();
}
私有void findviews()
{
searchStores=(AutoCompleteTextView)findviewbyd(R.id.autocomplete_存储);
}
私有void自动完成()
{
searchStoresString=getResources().getStringArray(R.array.stores\u数组);
searchStoresAdapter=new ArrayAdapter(此,R.layout.list_项,searchStoresString);
searchStores.setThreshold(1);
searchStores.setAdapter(searchStoresAdapter);
}
一切正常。谢谢您的建议。您是否尝试了
setKeyListener()
??有关更多信息,请尝试setKeyListener()
??有关更多信息,请尝试设置AutoCompleteTextView:
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;
}
});
请尝试设置AutoCompleteTextView:
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;
}
});
是的,但有错误,尝试了一些方法。我只是不知道如何使用AutoCompleteTextViews。是的,但出现了错误,我尝试了几种方法。我只是不知道如何使用AutoCompleteTextView,或者你可以使用setOnEditorActionListener for AutoCompleteTextView来获取EditorInfo.IME\u ACTION\u SEARCH ACTION KeyListener和EditorListener有什么区别?@RotaryHeary:我认为EditorListener I适用于EditView,所以尝试使用KeyListener不在这里工作。出于某种原因,听众从不开火。顺便说一下,您有一个(brakets)语法错误。是的,这两个事件侦听器都没有启动。我不明白为什么OP将此解决方案标记为答案。或者您可以使用setOnEditorActionListener for AutoCompleteTextView获取EditorInfo.IME\u ACTION\u SEARCH ACTION KeyListener和EditorListener有什么区别?@RotaryHeary:我认为EditorListener I用于EditView,所以尝试使用KeyListener在这里不工作。出于某种原因,听众从不开火。顺便说一下,您有一个(brakets)语法错误。是的,这两个事件侦听器都没有启动。我不明白为什么OP把这个解决方案标记为答案。