android EditText如何作为自动完成
我希望我的android EditText如何作为自动完成,android,android-edittext,Android,Android Edittext,我希望我的EditText应该作为AutoComplete工作,因为我是在XML文件中编写的 android:inputType="textAutoComplete|textAutoCorrect" 但它不起作用 我正在使用APIv2.2,我的活动扩展了MapActivity,在那里我放置了一个简单的EditText和一个名为“搜索”的按钮。因此,如果我们在EditText中键入位置名称,并按下搜索按钮,则表示它应该转到地图中的该位置。 因此,我希望EditText能够作为自动完成。 我该怎么
EditText
应该作为AutoComplete
工作,因为我是在XML文件中编写的
android:inputType="textAutoComplete|textAutoCorrect"
但它不起作用
我正在使用APIv2.2,我的活动扩展了MapActivity
,在那里我放置了一个简单的EditText
和一个名为“搜索”的按钮。因此,如果我们在EditText
中键入位置名称,并按下搜索按钮,则表示它应该转到地图中的该位置。
因此,我希望EditText
能够作为自动完成
。
我该怎么做呢?只需使用
自动完成文本视图
而不是普通的编辑文本
会有帮助的
编辑:上面的链接似乎已过期。新页面在这里:首先转换您的
EditText->AutoCompleteTextView
然后使用ArrayAdapter将XML文件链接到AutoCompleteTextView
假设您创建的XML字符串数组
命名为国家/地区列表
,则它可以链接到您的自动完成文本视图
,如下所示:
String[] countries = getResources().getStringArray(R.array.list_of_countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,countries);
actv.setAdapter(adapter);
String[]countries=getResources().getStringArray(R.array.list\u of_countries);
ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,国家);
actv.setAdapter(适配器);
DefaultArrayAdapter
仅按前几个字符过滤。如果您还想查看包含搜索关键字的单词,则需要使用自定义ArrayAdapter并覆盖其getView
和getFilter
方法。看看我在另一个StackOverflow问题中提供的完整解决方案:
一些代码片段:
public class AutoSuggestAdapter extends ArrayAdapter
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// handle view here
}
@Override
public Filter getFilter()
{
// implement filtering here
}
}
我使用以下代码:
1) 关于AndroidManifest.xml
<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>
此代码用于更改MultiAutoCompleteTextView的设置
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,codeKeyWords);
MultiAutoCompleteTextView autoCompleteTextView1 = (MultiAutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
autoCompleteTextView1.setAdapter(adapter);
autoCompleteTextView1.setThreshold(1);
autoCompleteTextView1.setTokenizer(new this.CommaTokenizer());
对不起,我首先从谷歌搜索到了AutoCompleteTextView。但我想可能还有其他的工作要做。这就是我问的原因。@Jyosna-你为什么想要另一种方式?您是否尝试过自动完成文本视图
?它不能解决你的问题吗?我想像谷歌搜索引擎一样,像我在那里写的任何东西一样,它应该自动完成。例如,我在location中输入了一些城市名称到一个xml文件中,AutoCompleteTextView只适用于我在String.xml的字符串数组中提到的那些城市名称。但我希望我的AutoCompleteTextview可以像谷歌搜索框一样工作。我怎么能做到这一点,你知道吗?我不知道谷歌是如何准确地生成自动完成的,但我相信它是在统计的基础上生成的。您可以做的是在用户每次搜索某个内容时将搜索查询保存到外部文件(或者您可以使用某种算法生成更好的建议),并在运行时从该文件加载自动完成字符串。只要谷歌安卓文件处理,我相信你会找到很多教程。这是一个很好的简单教程,它还包括通过Web服务自动完成。这只显示用户开始键入后的建议。另外,我不确定安卓m的新权限是否安全。
private ArrayAdapter<String> getEmailAddressAdapter(Context context) {
Account[] accounts = AccountManager.get(context).getAccounts();
String[] addresses = new String[accounts.length];
for (int i = 0; i < accounts.length; i++) {
addresses[i] = accounts[i].name;
}
return new ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line, addresses);
}
AutoCompleteTextView autoCompleteTextView1 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
autoCompleteTextView1.setAdapter(getEmailAddressAdapter(this));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,codeKeyWords);
MultiAutoCompleteTextView autoCompleteTextView1 = (MultiAutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
autoCompleteTextView1.setAdapter(adapter);
autoCompleteTextView1.setThreshold(1);
autoCompleteTextView1.setTokenizer(new this.CommaTokenizer());
/**
* This simple Tokenizer can be used for lists where the items are
* separated by a comma and one or more spaces.
*/
public static class CommaTokenizer implements Tokenizer {
public int findTokenStart(CharSequence text, int cursor) {
int i = cursor;
while (i > 0 && text.charAt(i - 1) != ' ') {
i--;
}
while (i < cursor && text.charAt(i) == '\n') {
i++;
}
return i;
}
public int findTokenEnd(CharSequence text, int cursor) {
int i = cursor;
int len = text.length();
while (i < len) {
if (text.charAt(i) == '\n') {
return i;
} else {
i++;
}
}
return len;
}
public CharSequence terminateToken(CharSequence text) {
int i = text.length();
while (i > 0 && text.charAt(i - 1) == ' ') {
i--;
}
if (i > 0 && text.charAt(i - 1) == ' ') {
return text;
} else {
if (text instanceof Spanned) {
SpannableString sp = new SpannableString(text + "\n");
TextUtils.copySpansFrom((Spanned) text, 0, text.length(),
Object.class, sp, 0);
return sp;
} else {
return text + " ";
}
}
}