Android的,Autocomplettextview,力文是从参赛名单是从、名单、Android、Autocomplettextview

2023-09-05 06:57:10 作者:小姐,伱是演妓派

有没有办法来迫使一autocompletetextview项是在entrylist的元素?

Is there a way to force the entry in an autocompletetextview to be one of the elements in the entrylist?

我发现了一个名为performValidation的方法,但林不知道它实际上做了,我还没有能够找到太多的文档或例子。

I've found a method called "performValidation" but im not sure what it actually does, and i havent been able to find much documentation or any examples.

推荐答案

AutoCompleteTextView setValidator将()称为方法这需要作为参数接口 AutoCompleteTextView.Validator 的实例。 AutoCompleteTextView.Validator 包含的isValid()与您可以检查已输入的值,并且可以修复这个字符串通过实施 fixText()

The AutoCompleteTextView has a method called setValidator() that takes an instance of the interface AutoCompleteTextView.Validator as parameter. AutoCompleteTextView.Validator contains isValid() with which you can check the value that has been entered, and you can "fix" this string by implementing fixText().

看来这是最好的,你可以用 AutoCompleteTextView 获得,作为文档 AutoCompleteTextView.Validator 规定如下

Seems this is the best you can get with AutoCompleteTextView, as the documentation for AutoCompleteTextView.Validator states the following:

因为没有万无一失的方法   $ P $离开这个pvent用户   看在它的值不正确,   我们所能做的就是尽力解决这个问题   自己当这种情况发生。

"Since there is no foolproof way to prevent the user from leaving this View with an incorrect value in it, all we can do is try to fix it ourselves when this happens."

如果你的元素列表不是很长,你最好还是不要使用微调。

If your list of elements is not too long, you are probably better off using a Spinner.

******编辑:******

我扫线一起​​如何使用这一个简单的例子,希望它能帮助!

I wipped together a quick example of how you can use this, hope it helps!

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<AutoCompleteTextView  
    android:id="@+id/input"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Focus me to validate above text"/>
</LinearLayout>

-

public class AutoCompleteTextViewActivity extends Activity {

    String[] validWords = new String[]{"", "snowboard", "bobsleigh", "slalom"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        AutoCompleteTextView view = (AutoCompleteTextView)findViewById(R.id.input);
        view.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, validWords));
        view.setValidator(new Validator());
        view.setOnFocusChangeListener(new FocusListener());
    }

    class Validator implements AutoCompleteTextView.Validator {

        @Override
        public boolean isValid(CharSequence text) {
            Log.v("Test", "Checking if valid: "+ text);
            Arrays.sort(validWords);
            if (Arrays.binarySearch(validWords, 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.input && !hasFocus) {
                Log.v("Test", "Performing validation");
                ((AutoCompleteTextView)v).performValidation();
            }
        }
    }
}