Android, Autocomplettextview, Force Text To Be From The Entry List
Solution 1:
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()
.
Seems this is the best you can get with AutoCompleteTextView
, as the documentation for AutoCompleteTextView.Validator
states the following:
"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.
****** Edit: ******
I wipped together a quick example of how you can use this, hope it helps!
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"
><AutoCompleteTextViewandroid:id="@+id/input"android:layout_width="fill_parent"android:layout_height="wrap_content"
/><EditTextandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="Focus me to validate above text"/></LinearLayout>
-
publicclassAutoCompleteTextViewActivityextendsActivity {
String[] validWords = newString[]{"", "snowboard", "bobsleigh", "slalom"};
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AutoCompleteTextView view = (AutoCompleteTextView)findViewById(R.id.input);
view.setAdapter(newArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, validWords));
view.setValidator(newValidator());
view.setOnFocusChangeListener(newFocusListener());
}
classValidatorimplementsAutoCompleteTextView.Validator {
@OverridepublicbooleanisValid(CharSequence text) {
Log.v("Test", "Checking if valid: "+ text);
Arrays.sort(validWords);
if (Arrays.binarySearch(validWords, text.toString()) > 0) {
returntrue;
}
returnfalse;
}
@OverridepublicCharSequencefixText(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"";
}
}
classFocusListenerimplementsView.OnFocusChangeListener {
@OverridepublicvoidonFocusChange(View v, boolean hasFocus) {
Log.v("Test", "Focus changed");
if (v.getId() == R.id.input && !hasFocus) {
Log.v("Test", "Performing validation");
((AutoCompleteTextView)v).performValidation();
}
}
}
}
Solution 2:
Another alternate way(comments are mentioned inline) :
AutoCompleteTextViewtxt_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 = newArrayAdapter<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(newAutoCompleteTextView.Validator() {
@OverridepublicbooleanisValid(CharSequence text){
//some logic here returns true or false based on if the text is validatedif(text == "This is what I want")
returntrue;
elsereturnfalse;
}
@Overridepublic 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 itreturn"This is what I want"
}
});
Reference: AutoCompleteTextView.Validator
Post a Comment for "Android, Autocomplettextview, Force Text To Be From The Entry List"