如何避免在AutoCompleteTextView上同时调用onItemClicked和onTextChanged

时间:2011-08-14 07:09:17

标签: android autocompletetextview textchanged

我有这个代码。当我从建议列表中选择一个项目时,onTextChanged首先发生,然后oItemClicked在此之后。现在我想在选择单词时首先出现“onItemClicked”,然后出现“onTextChanged”。我看了一下Android doc,但没有提到这个话题。

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) {

    }
}

2 个答案:

答案 0 :(得分:82)

isPerformingCompletion()在API级别3中添加。在从下拉列表中选择项目并且 TextWatcher 即将被触发后,返回 true 。总之,要避免所描述的行为:

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
}

答案 1 :(得分:0)

这个问题没有特别的解决方案。我建议你应该在textchangedlistener()的开头放置onclicklistener()里面的代码,这样你想要先执行的代码先执行然后执行你想要最后执行的代码将执行last.Hope这将对你有帮助。