autocompletetextviewのエントリをエントリリストの要素の1つにする方法はありますか?
「performValidation」というメソッドを見つけましたが、実際に何をするのかわかりません。また、多くのドキュメントや例を見つけることができませんでした。
autocompletetextviewのエントリをエントリリストの要素の1つにする方法はありますか?
「performValidation」というメソッドを見つけましたが、実際に何をするのかわかりません。また、多くのドキュメントや例を見つけることができませんでした。
には、インターフェイスのインスタンスをパラメーターとして受け取る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();
}
}
}
}
別の代替方法(コメントはインラインで言及されています):
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"
}
});