我用谷歌搜索了一下,我得到了解决方案。我做了如下代码及其对我的工作。我只是在我们的 Activity onCreate() 方法中设置了自动完成。
在我的例子中,我使用@
字符来识别应该查找的文本片段的开头和空格来确定查找标记的结尾。
public class MultiAutoCompleteTextViewDemo extends AppCompatActivity
{
MultiAutoCompleteTextView inputEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
inputEditText = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);
inputEditText.setTokenizer(new UsernameTokenizer());
inputEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int cursor = start;
if (cursor >= s.length()) cursor = s.length()-1;
if (isValidToken(s, cursor)){
String token = getToken(s, start);
new LinkedinSkillAsyncTask((Activity) getApplicationContext()).execute( token );
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
private boolean isValidToken(CharSequence text, int cursor){
for (int i=cursor; i>=0; i--){
if (text.charAt(i) == '@') return true;
if (text.charAt(i) == ' ') return false;
}
return false;
}
private String getToken(CharSequence text, int cursor){
int start=findTokenStart(text, cursor);
int end=findTokenEnd(text, cursor);
return text.subSequence(start, end).toString();
}
private int findTokenStart(CharSequence text, int cursor) {
int i = cursor;
while (i > 0 && text.charAt(i - 1) != '@') { i--; }
return i;
}
private int findTokenEnd(CharSequence text, int cursor) {
int i = cursor;
int len = text.length();
while (i < len && text.charAt(i) != ' ' && text.charAt(i) != ',' && text.charAt(i) != '.' ) {
i++;
}
return i;
}
public class UsernameTokenizer implements MultiAutoCompleteTextView.Tokenizer {
@Override public CharSequence terminateToken(CharSequence text) {
int i = text.length();
while (i > 0 && text.charAt(i - 1) == ' ') { i--; }
if (text instanceof Spanned) {
SpannableString sp = new SpannableString(text + " ");
TextUtils.copySpansFrom((Spanned) text, 0, text.length(), Object.class, sp, 0);
return sp;
} else {
return text + " ";
}
}
@Override public int findTokenStart(CharSequence text, int cursor) {
int i = cursor;
while (i > 0 && text.charAt(i - 1) != '@') {
i--;
}
if (i < 1 || text.charAt(i - 1) != '@') {
return cursor;
}
return i;
}
@Override public int findTokenEnd(CharSequence text, int cursor) {
int i = cursor;
int len = text.length();
while (i < len) {
if (text.charAt(i) == ' ') {
return i;
} else {
i++;
}
}
return len;
}
}
public class LinkedinSkillAsyncTask extends AsyncTask<String, String, String> {
private Activity context;
public String data;
public List<String> suggest;
public ArrayAdapter<String> aAdapter;
public LinkedinSkillAsyncTask(Activity cntxt) {
context = cntxt;
}
@Override protected String doInBackground(String... key) {
String newText = key[0];
newText = newText.trim();
newText = newText.replace(" ", "+");
try {
HttpClient hClient = new DefaultHttpClient();
HttpGet hGet = new HttpGet("http://www.linkedin.com/ta/skill?query="+newText);
ResponseHandler<String> rHandler = new BasicResponseHandler();
data = hClient.execute(hGet, rHandler);
suggest = new ArrayList<String>();
JSONObject jobj = new JSONObject(data);
JSONArray jArray = jobj.getJSONArray("resultList");
for (int i = 0; i < jArray.length(); i++) {
String SuggestKey = jArray.getJSONObject(i).getString("displayName");
suggest.add(SuggestKey);
}
} catch (Exception e) {
Log.w("Error", e.getMessage());
}
context.runOnUiThread(new Runnable() {
public void run() {
MultiAutoCompleteTextView inputEditText = (MultiAutoCompleteTextView) context.findViewById(R.id.multiAutoCompleteTextView1);
aAdapter = new ArrayAdapter<String>( context, android.R.layout.simple_dropdown_item_1line, suggest);
inputEditText.setAdapter(aAdapter);
aAdapter.notifyDataSetChanged();
}
});
return null;
}
}
}
有关更多详细信息,您可以阅读本教程。例如查看这个GitHub演示项目。