validation - Android, Autocomplettextview, force text to be from the entry list -
is there way force entry in autocompletetextview 1 of elements in entrylist?
i've found method called "performvalidation" im not sure does, , havent been able find documentation or examples.
the autocompletetextview
has method called setvalidator()
takes instance of interface autocompletetextview.validator
parameter. autocompletetextview.validator
contains isvalid()
can check value has been entered, , can "fix" string implementing fixtext()
.
seems best can autocompletetextview
, documentation autocompletetextview.validator
states following:
"since there no foolproof way prevent user leaving view incorrect value in it, can try fix ourselves when happens."
if list of elements not long, better off using spinner.
****** edit: ******
i wipped quick example of how can use this, hope 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 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 returning empty string here, field blanked, * put kind of action here, popping dialog? * * whatever value return here must in 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(); } } } }
Comments
Post a Comment