2015-05-04 38 views
7

我想在用戶從自動完成建議中選擇文本時添加空格,所以當他繼續輸入時,他會從一個新單詞開始。如何給AutoCompleteTextView添加空間建議

我試圖用TextWatcher來做,但我得到了IndexOutOfBoundsException

有什麼建議嗎?

enter image description here

我使用的文本Watcher是:

private class AddSpaceTextWatcher implements TextWatcher{ 

    boolean shouldAddSpace = false; 

    @Override 
    public void beforeTextChanged(CharSequence s, int start, int before, int count) { 

    } 

    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
     if (count - before > 1) // check that the new input is not from the keyboard 
      shouldAddSpace = true; 
    } 

    @Override 
    public void afterTextChanged(Editable editable) { 
     if (shouldAddSpace) { 
      shouldAddSpace = false; 
      mAutoCompleteTextView.setText(" "); 
     } 
    } 
} 

我得到的例外是:

java.lang.IndexOutOfBoundsException: setSpan (18 ... 18) ends beyond length 1 
     at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1018) 
     at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:611) 
     at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:607) 
     at android.text.Selection.setSelection(Selection.java:76) 
     at android.text.Selection.setSelection(Selection.java:87) 
     at android.widget.EditText.setSelection(EditText.java:99) 
     at android.widget.SearchView.setQuery(SearchView.java:1465) 
     at android.widget.SearchView.onQueryRefine(SearchView.java:889) 
     at android.widget.SuggestionsAdapter.onClick(SuggestionsAdapter.java:371) 
     at android.view.View.performClick(View.java:4756) 
     at android.view.View$PerformClick.run(View.java:19748) 
     at android.os.Handler.handleCallback(Handler.java:739) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5254) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693) 
+1

你可以發佈異常的代碼和堆棧跟蹤嗎? – Blackbelt

+0

也許你需要調用append而不是setText – Blackbelt

+0

你可以發佈你的arrayAdapter代碼.. –

回答

3

對於選擇多個項目,您可以簡單地使用MultiAutoCompleteTextView這是專門爲此建立的AutoCompleteTextView一個子類。

OR

如果你只是想在最後追加一個空間做只有一個項目選擇比試試這個:

public void afterTextChanged(Editable editable) { 
    String text = editable.toString(); 
    if (shouldAddSpace && text.length() > 0 && !text.endsWith(" ")) { 
     editable.append(' '); 
    } 
} 

編輯:

一旦空間被追加,您只需撥打mAutoCompleteTextView.showDropDown();即可再次下載更新的文本。

+0

@Pinhassi對你有用嗎?如果你遇到任何問題,請讓我知道... –

+0

它的確如此,所以我會給你賞金,但是我也想讓光標移動到文本的末尾(在「」之後)。 –

+0

@Pinhassi看到我更新的遊標更新的答案,並讓我知道如果這不起作用,我還沒有測試,但它應該工作! –

0

我不知道這是否會解決您的問題,但mAutoCompleteTextView.setText(" ");會刪除文字。所以我這樣做:

mAutoCompleteTextView.setText(mAutoCompleteTextView.getText().toString() + " "); 
0
mAutoCompleteTextView.setText(" "); 

此代碼將文本視圖文本設置爲「」 - 您想要的是現有文本加空格。

mAutoCompleteTextView.setText(mAutoCompleteTextView.getText().append(" ")); 
0

我更喜歡使用「MultiAutoCompleteTextView」來實現您正在嘗試做的事情。

<AutoCompleteTextView 
    android:id="@+id/autoCompleteTextView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="65dp" 
    android:ems="10" > 

在java中的文件:

private AutoCompleteTextView actv; 
actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1); 

列表字符串:

String[] countries = getResources(). 
    getStringArray(R.array.list_of_countries); 
    ArrayAdapter adapter = new ArrayAdapter 
    (this,android.R.layout.simple_list_item_1,countries); 
    actv.setAdapter(adapter); 

對應於用戶輸入國的這個例子顯示列表。 我希望這個例子能對你的工作有所幫助。