如何限制AutoCompleteTextView下拉列表?

时间:2014-02-07 10:27:10

标签: android drop-down-menu textview autocompletetextview dismiss

我正在研究AutoCompleteTextView。当用户输入AutoCompleteTextView时,我会得到一些结果,这些是必须选择的。 但问题是当点击srceen的任何地方时,下拉自动解除。 我想避免这种情况。 有什么方法可以达到这个目的。

感谢。

2 个答案:

答案 0 :(得分:0)

尝试以下代码。

我使用AutoCompleteText自动完成用户当前所在的位置,locationList只是我在strings.xml文件中写的数组,所以在这里使用你自己的字符串数组。

  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();
            }
        });

及以下是用于验证位置字段中的文本输入的代码,fixText()方法阻止用户键入字符串数组中不存在的文本,例如:如果用户键入&#34; germany& #34;在你的字符串数组列表中不存在,它将替换为&#34; &#34;这是edittext输入字段中的空字符串

 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();
            }
        }
    }

答案 1 :(得分:0)

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 AutoCompleteTextView中反思,但是 - 也许这不是一个好的解决方案