【发布时间】:2017-05-20 14:56:37
【问题描述】:
我目前正在制作一个 android 应用程序,从一些固定的字符串数组自动完成。
当用户按下自动完成文本视图时,我希望默认打开一个带有“当前位置”选项的菜单(在用户开始输入之前),但我没有使用 Google Places(如自动完成的值来自数组),那么我该怎么做呢?非常感谢!
【问题讨论】:
标签: android autocomplete location
我目前正在制作一个 android 应用程序,从一些固定的字符串数组自动完成。
当用户按下自动完成文本视图时,我希望默认打开一个带有“当前位置”选项的菜单(在用户开始输入之前),但我没有使用 Google Places(如自动完成的值来自数组),那么我该怎么做呢?非常感谢!
【问题讨论】:
标签: android autocomplete location
您要在AutocompleteTextView 中显示default suggestion。
假设您有一个名为 locationAutoComplete 的 AutocompleteTextView。 您可以通过
显示默认建议 locationAutoComplete.setText("current location");
但是在AutocompleteTextview 中使用setText(String str) 显示default suggestion 存在问题;直接方法。每当调用setText(String str) 方法时,它都会禁用AutocompleteTextView。
为了防止它写你的代码如下。
locationAutoComplete.postDelayed(new Runnable() {
@Override
public void run() {
locationAutoComplete.showDropDown();
}
},500);
locationAutoComplete.setText("current location");
locationAutoComplete.setSelection(locationAutoComplete.getText().length());
【讨论】:
如果你只使用一组固定的字符串,你可以使用AutoCompleteTextView:
String[] yourStrings; // However you get your strings
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, yourStrings);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.your_text_view);
textView.setAdapter(adapter);
【讨论】: