Syn*_*ete 8 android textview autocompletetextview dismiss drop-down-menu
我正在努力AutoCompleteTextView.当用户输入AutoCompleteTextView并且必须选择这些结果时,我会得到一些结果.但问题是当点击srceen的任何地方时,下拉自动解除.我想避免这种情况.有什么方法可以实现这一点.
谢谢.
小智 0
尝试下面的代码。
我正在使用 AutoCompleteText 自动完成用户当前所在的位置,locationList 只是我在 strings.xml 文件中编写的一个数组,因此在这里使用您自己的字符串数组。
locationList = res.getStringArray(R.array.ticketLocation);
ArrayAdapter<String> locationAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, locationList);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txtCountries);
textView.setThreshold(1);
textView.setAdapter(locationAdapter);
textView.setValidator(new Validator());
textView.setOnFocusChangeListener(new FocusListener());
textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
TextView ticketLocation = (TextView) view;
getTicketLocation = ticketLocation.getText().toString();
}
});
Run Code Online (Sandbox Code Playgroud)
下面是验证位置字段中文本输入的代码,fixText() 方法会阻止用户键入字符串数组中不存在的文本,例如:如果用户键入不存在于中的“germany”您的字符串数组列表,它将被替换为“”,它是您的编辑文本输入字段中的空字符串
class Validator implements AutoCompleteTextView.Validator {
@Override
public boolean isValid(CharSequence text) {
// Log.v("Test", "Checking if valid: " + text);
Arrays.sort(locationList);
if (Arrays.binarySearch(locationList, 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.txtCountries && !hasFocus) {
// Log.v("Test", "Performing validation");
((AutoCompleteTextView) v).performValidation();
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1151 次 |
| 最近记录: |