我已经实现了自动完成编辑文本,它确实显示了建议,但我也希望实现不同的功能。
现在,我有一串国家
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
我在我的适配器中调用它。现在,当我开始输入任何国家/地区名称时,我的编辑文本开始向我显示建议,但
我想要的是:
让我们说我想在编辑文本中写10次法国,所以我希望我的自动完成编辑文本给我建议,当我开始输入法国它等等时,
但是我的编辑文字只给出了我输入的第一个单词的建议。我怎样才能实现这一点,比如我的编辑文本如何为我输入的每个文本提供建议?
答案 0 :(得分:4)
检查以下代码,它会像按空格键入内容一样,您将获得建议列表:
Xml布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<MultiAutoCompleteTextView
android:id="@+id/macTvInput"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
代码:
public class MainActivity extends Activity {
private static final String[] COUNTRIES = new String[] { "Belgium",
"France", "Italy", "Germany", "Spain" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
MultiAutoCompleteTextView macTvInput = (MultiAutoCompleteTextView) findViewById(R.id.macTvInput);
macTvInput.setAdapter(adapter);
macTvInput.setTokenizer(new SpaceTokenizer());
}
private class SpaceTokenizer implements Tokenizer {
private final char delimiter = ' ';
public int findTokenStart(CharSequence text, int cursor) {
int i = cursor;
while (i > 0 && text.charAt(i - 1) != delimiter) {
i--;
}
while (i < cursor && text.charAt(i) == delimiter) {
i++;
}
return i;
}
public int findTokenEnd(CharSequence text, int cursor) {
int i = cursor;
int len = text.length();
while (i < len) {
if (text.charAt(i) == delimiter) {
return i;
} else {
i++;
}
}
return len;
}
public CharSequence terminateToken(CharSequence text) {
int i = text.length();
while (i > 0 && text.charAt(i - 1) == delimiter) {
i--;
}
return text;
}
}
}
答案 1 :(得分:0)
在你的版面中使用MultiAutoCompleteTextView
,并实现这样的代码,在这里我曾用commas
分割
String[] colors = getResources().getStringArray(R.array.AndroidDesk);
adapter1 = new ArrayAdapter<String>(this,R.layout.suggestion_textview,colors);
//autoComplete = (AutoCompleteTextView) findViewById(R.id.autoComplete);
multiAutoComplete = (MultiAutoCompleteTextView) findViewById(R.id.editText1);
// set adapter for the auto complete fields
// autoComplete.setAdapter(adapter);
multiAutoComplete.setAdapter(adapter1);
// specify the minimum type of characters before drop-down list is shown
//autoComplete.setThreshold(1);
multiAutoComplete.setThreshold(2);
// comma to separate the different colors
multiAutoComplete.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
答案 2 :(得分:0)
使用MultiAutoCompleteTextView代替AutoCompleteTextView
public class CountriesActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autocomplete_7);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
MultiAutoCompleteTextView textView = (MultiAutoCompleteTextView) findViewById(R.id.edit);
textView.setAdapter(adapter);
textView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
}
来自:http://developer.android.com/reference/android/widget/MultiAutoCompleteTextView.html