19

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

我找到了一种名为“performValidation”的方法,但我不确定它实际上做了什么,而且我找不到太多文档或任何示例。

4

2 回答 2

26

AutoCompleteTextView有一个调用的方法,该方法setValidator()将接口的实例AutoCompleteTextView.Validator作为参数。AutoCompleteTextView.Validator包含isValid()您可以使用它检查已输入的值,并且您可以通过实现来“修复”此字符串fixText()

似乎这是您可以获得的最好的AutoCompleteTextView,因为文档AutoCompleteTextView.Validator说明如下:

“由于没有万无一失的方法可以防止用户离开此视图时的值不正确,因此我们所能做的就是在发生这种情况时尝试自己修复它。”

如果您的元素列表不太长,则最好使用 Spinner。

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

我整理了一个如何使用它的简单示例,希望对您有所帮助!

<?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();
            }
        }
    }
}
于 2011-02-17T19:51:27.760 回答
0

另一种替代方式(内联提到评论):

AutoCompleteTextView txt_site_name  = findViewById(R.id.some_auto_text);
// Get the string array for the countries
String[] countries = getResources().getStringArray(R.array.ncr_parking_list_array);
// Create the adapter and set it to the AutoCompleteTextView
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_dropdown_item_1line, countries);
// txt_site_name  is name of the AutoComplete text view. or AutoCompleteTextView txt_site_name
txt_site_name.setAdapter(adapter);

txt_site_name.setValidator(new AutoCompleteTextView.Validator() {
     @Override
     public boolean isValid (CharSequence text){
          //some logic here returns true or false based on if the text is validated
          if(text == "This is what I want") 
              return true;
          else 
              return false;
     }

     @Override
     public CharSequence fixText (CharSequence invalidText){
          //If .isValid() returns false then the code comes here
          //do whatever way you want to fix in the users input and  return it
          return "This is what I want"
            }
        });

参考:AutoCompleteTextView.Validator

于 2015-02-11T17:36:54.213 回答