如何限制AutoCompleteTextView下拉菜单的关闭?

8
我正在处理一个 AutoCompleteTextView 。当用户在AutoCompleteTextView中输入文本时,会出现一些结果,这些结果是必须选择的。 但问题是,当在屏幕上的任何地方单击时,下拉列表会自动消失。 我想避免这种情况。 有没有办法可以实现这个需求。
谢谢。

@Andreyua,请尝试此链接:https://matalamaki.fi/2015/09/07/android-autocompletetextview-with-drop-down-always-visible-or-how-to-figure-your-way-with-internal-android-apis/。另一种方法是在`onTouchEvent(MotionEvent event)内调用autoTextView.showDropDown();`,然而,它的效果并不美观 :) - BNK
你可以保留一个变量来跟踪下拉菜单是否可见,并在你的活动中实现 onTouchEvent。如果下拉菜单可见,则返回 true 并且不调用 super,否则调用 super。也许它会起作用,但这不是一个好的解决方案。 - Ankit Aggarwal
2个回答

1
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 中进行反射,但是 - 也许这并不是一个好的解决方案。


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() 方法可以防止用户输入不存在于字符串数组中的文本。例如:如果用户输入了“德国”,而该字符串不在您的字符串数组列表中,则它将被替换为“ ”(即空字符串)并显示在您的编辑框输入字段中。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接