Android,Autocomplettextview,强制文本来自条目列表
问题描述:
有没有办法强制自动完成文本视图中的条目是其中的一个元素?Android,Autocomplettextview,强制文本来自条目列表
我找到了一个名为“performValidation”的方法,但我不知道它实际做了什么,而且我还没有找到很多文档或任何示例。
答
AutoCompleteTextView
有一个名为setValidator()
的方法,它将接口AutoCompleteTextView.Validator
的一个实例作为参数。 AutoCompleteTextView.Validator
包含isValid()
,您可以使用它检查输入的值,并且可以通过执行fixText()
来“修复”该字符串。
看来,这是你可以用AutoCompleteTextView
得到最好的,作为AutoCompleteTextView.Validator
状态下的文件:
“既然是 没有万无一失的方法阻止用户离开与不正确的这个 查看值, 我们所能做的就是在发生这种情况时自己修复它 “。
如果你的元素列表不是太长,你最好使用微调。
******编辑:******
我wipped在一起的,你如何使用这个一个简单的例子,希望它帮助!
<?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 true;}
}
@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验证代码
String str = clientName.getText().toString();
ListAdapter listAdapter = clientName.getAdapter();
for(int i = 0; i < listAdapter .getCount(); i++) {
String temp = listAdapter .getItem(i).toString();
if(str.compareTo(temp) == 0)
{
return;
}
}
欲了解更多信息click here
感谢。最初我使用一个微调,但我的用户抱怨,所以ID认为ID将其更改为ACTV。你有没有提到验证器已被使用的例子?我正在努力实现。 – Drublic 2011-02-17 19:59:50