2011-08-14 41 views
20

我有這段代碼。當我從建議列表中選擇一個項目時,首先發生onTextChanged,然後是oItemClicked。現在我想要選擇一個單詞時,「onItemClicked」首先出現,然後是「onTextChanged」。我看了一下Android文檔,但沒有提到這個話題。如何避免在AutoCompleteTextView上調用onItemClicked和onTextChanged

package com.autocompletetest; 


import java.util.Arrays; 
import java.util.List; 

import android.app.Activity; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.AutoCompleteTextView; 

public class OnItemClickAndOnTextChangedActivity extends Activity implements TextWatcher, OnItemClickListener { 
    private AutoCompleteTextView textView; 

    private static final String[] TEMP = new String[] { 
     "Beep", "Belgium", "Best", "Bubble", "Bye" 
    }; 
    @Override 
    protected void onCreate(final Bundle icicle) { 
     super.onCreate(icicle); 
     setContentView(R.layout.main); 

     textView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1); 
     textView.setOnItemClickListener(this); 
     textView.addTextChangedListener(this); 
     textView.setThreshold(1); 

     final List<String> list = Arrays.asList(TEMP); 

     final ArrayAdapter<String> adapter = new ArrayAdapter<String>(
       OnItemClickAndOnTextChangedActivity.this, 
       android.R.layout.simple_dropdown_item_1line, list); 
     textView.setAdapter(adapter); 
    } 

    @Override 
    public void onTextChanged(final CharSequence s, final int start, final int before, final int count) { 
     System.out.println("OnTextChanged."); 
    } 

    @Override 
    public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) { 
     System.out.println("OnItemClick."); 
    } 

    @Override 
    public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) { 
    } 

    @Override 
    public void afterTextChanged(final Editable s) { 

    } 

} 

更新: 具體而言,這是我想要做真實的東西。有些東西與問題標題無關。

package com.autocompletetest; 


import java.util.Arrays; 
import java.util.List; 

import android.app.Activity; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.AutoCompleteTextView; 

public class OnItemClickAndOnTextChangedActivity extends Activity implements TextWatcher, OnItemClickListener { 
    private AutoCompleteTextView textView; 
    private boolean itemClicked; 

    private static final String[] TEMP = new String[] { 
     "Beep", "Belgium", "Best", "Bubble", "Bye" 
    }; 
    @Override 
    protected void onCreate(final Bundle icicle) { 
     super.onCreate(icicle); 
     setContentView(R.layout.main); 

     textView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1); 
     textView.setOnItemClickListener(this); 
     textView.addTextChangedListener(this); 
     textView.setThreshold(1); 

     final List<String> list = Arrays.asList(TEMP); 

     final ArrayAdapter<String> adapter = new ArrayAdapter<String>(
       OnItemClickAndOnTextChangedActivity.this, 
       android.R.layout.simple_dropdown_item_1line, list); 
     textView.setAdapter(adapter); 
    } 

    @Override 
    public void onTextChanged(final CharSequence s, final int start, final int before, final int count) { 
     System.out.println("OnTextChanged."); 

     // The below code block does: 
     // When type a word, make a new ArrayAdapter and set it for textView 
     // If any of word in suggestion list is clicked, nothing changes, dropdown list not shown. 
     if(itemClicked) { 
      itemClicked = false; 
     } else { 
      // Create new ArrayAdapter. 
      // textView is set to new ArrayAdapter. 
      // textView.showDropDown() 
     } 
    } 

    @Override 
    public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) { 
     System.out.println("OnItemClick."); 
     itemClicked = true; 
    } 

    @Override 
    public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) { 
    } 

    @Override 
    public void afterTextChanged(final Editable s) { 

    } 
} 
+0

您是否在'beforeTextChanged()'調用時檢查。 – Ronnie

+0

爲什麼不能在OnclickListener()中做所有的事情? – Ronnie

+0

是的,我做到了。 TextWatcher的所有功能都在onItemClick之前發生。 – Emerald214

回答

75

isPerformingCompletion()在API級別3.返回加入一個項目已經從下拉列表中選擇和TextWatcher聽衆即將被觸發後。簡而言之,爲避免描述的行爲:

public void onTextChanged(CharSequence s, int start, int before, int count) { 
    if (autoCompleteView.isPerformingCompletion()) { 
     // An item has been selected from the list. Ignore. 
     return; 
    } 

    // Your code for a general case 
} 
+0

謝謝,正是我在找的! – Marco

+0

完美運作!謝謝,Neek! – Scit

+0

不錯,謝謝 – Imotep

0

有這個疑難問題沒有特別的解決方案會建議你應該把代碼放在這裏面onclicklistener()在textchangedlistener(的開頭),因此,美想要的代碼得到執行第一將首先執行,然後你想要執行的代碼將最後執行。希望這會對你有幫助。

+0

其實我有一個布爾變量「itemClicked」,當點擊建議列表中的一個項目時,它會是true。每次文本更改時我都要檢查「itemClicked」。問題是..單擊一個單詞後,onTextChanged發生在onItemClicked之前。 – Emerald214

+0

你可以做一件事情。正如我所說的那樣,有一種特殊的解決方案,因爲textchange將首先被調用。因此,你將你的代碼放在函數內的textChangedListener()中,並在onItemClicked()的末尾調用該函數。我認爲工作。 –