如何限制AutoCompleteTextView下拉closures?

我正在使用AutoCompleteTextView 。 当用户inputAutoCompleteTextView时,我会得到一些结果,这些都是必须select的。 但问题是,当在srceen中的任何地方点击时,下拉菜单会自动解除。 我想避免这一点。 有什么办法可以做到这一点吗?

谢谢。

Solutions Collecting From Web of "如何限制AutoCompleteTextView下拉closures?"

尝试下面的代码。

我使用AutoCompleteText来自动完成用户当前所在的位置,locationList只是一个数组,我在strings.xml文件中写入,所以在这里使用自己的string数组。

  locationList = res.getStringArray(R.array.ticketLocation); ArrayAdapter<String> locationAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, locationList); AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txtCountries); textView.setThreshold(1); textView.setAdapter(locationAdapter); textView.setValidator(new Validator()); textView.setOnFocusChangeListener(new FocusListener()); textView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub TextView ticketLocation = (TextView) view; getTicketLocation = ticketLocation.getText().toString(); } }); 

下面是在位置字段中validation文本input的代码,fixText()方法可以防止用户inputstring数组中不存在的文本,例如:如果用户键入的“germany”不存在于你的string数组列表,它将被replace为你的edittextinput字段中的空string“”

  class Validator implements AutoCompleteTextView.Validator { @Override public boolean isValid(CharSequence text) { // Log.v("Test", "Checking if valid: " + text); Arrays.sort(locationList); if (Arrays.binarySearch(locationList, text.toString()) > 0) { return true; } return false; } @Override public CharSequence fixText(CharSequence invalidText) { // Log.v("Test", "Returning fixed text"); /* * I'm just returning an empty string here, so the field will be * blanked, but you could put any kind of action here, like popping * up a dialog? * * Whatever value you return here must be in the list of valid * words. */ return ""; } } class FocusListener implements View.OnFocusChangeListener { @Override public void onFocusChange(View v, boolean hasFocus) { // Log.v("Test", "Focus changed"); if (v.getId() == R.id.txtCountries && !hasFocus) { // Log.v("Test", "Performing validation"); ((AutoCompleteTextView) v).performValidation(); } } } 
 private boolean setForceIgnoreOutsideTouchWithReflexion(boolean forceIgnoreOutsideTouch) { try { Method method = android.widget.AutoCompleteTextView.class.getMethod("setForceIgnoreOutsideTouch", boolean.class); method.invoke(this, forceIgnoreOutsideTouch); return true; } catch (Exception e) { return false; } } 

只有在public class CustomAutoCompleteTextView extends AutoCompleteTextViewreflectionpublic class CustomAutoCompleteTextView extends AutoCompleteTextView ,但 – 也许这也不是一个好的解决scheme