本文介绍了Android,Autocomplettextview,强制文本来自条目列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法强制自动完成文本视图中的条目成为条目列表中的元素之一?

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:

"因为没有万无一失的方法防止用户离开这个视图中的值不正确,我们所能做的就是尝试修复它发生这种情况时我们自己."

如果您的元素列表不太长,您可能最好使用 Spinner.

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

这篇关于Android,Autocomplettextview,强制文本来自条目列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 12:20