Android 如何限制AutoCompleteTextView下拉列表?

Android 如何限制AutoCompleteTextView下拉列表?,android,drop-down-menu,textview,autocompletetextview,dismiss,Android,Drop Down Menu,Textview,Autocompletetextview,Dismiss,我正在开发一个自动完成文本视图。当用户输入AutoCompleteTextView时,我会得到一些结果,这些结果是必须选择的。 但问题是,当点击srceen中的任意位置时,下拉列表会自动关闭。 我想避免这种情况。 我有没有办法做到这一点 谢谢。试试下面的代码 我正在使用AutoCompleteText自动完成用户当前所在的位置,locationList只是我在strings.xml文件中编写的一个数组,所以在这里使用您自己的字符串数组 locationList = res.getString

我正在开发一个
自动完成文本视图
。当用户输入
AutoCompleteTextView
时,我会得到一些结果,这些结果是必须选择的。 但问题是,当点击srceen中的任意位置时,下拉列表会自动关闭。 我想避免这种情况。 我有没有办法做到这一点

谢谢。

试试下面的代码

我正在使用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();
            }
        });

只有
公共类CustomAutoCompleteTextView中的反射扩展了AutoCompleteTextView,但是-也许这也不是一个好的解决方案

@Andreyua试试这个。另一种方法是调用
autoTextView.showDropDown()
在onTouchEvent(MotionEvent事件)
内部,但是,它的效果并不漂亮:)您可以保留一个变量来跟踪下拉列表是否可见,并在活动中实现onTouchEvent。如果下拉列表可见,则返回true,不调用super,否则调用super。也许它会起作用,但这不是一个好的解决方案
 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;
    }
}