如何避免同时被调用:AutoCompleteTextView上的onItemClicked和onTextChanged [英] How to avoid getting both called: onItemClicked and onTextChanged on AutoCompleteTextView

查看:86
本文介绍了如何避免同时被调用:AutoCompleteTextView上的onItemClicked和onTextChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码.当我从建议列表中选择一个项目时,首先发生onTextChanged,然后是oItemClicked.现在,我想要选择一个单词时,首先出现"onItemClicked",然后出现"onTextChanged".我看了看Android文档,但没有提到这个话题.

I have this code. When I choose an item from suggestion list, the onTextChanged happens first, then oItemClicked comes after that. Now I want when choosing a word, the "onItemClicked" appears first, then "onTextChanged". I took a look Android doc but it doesn't mention this topic.

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

    }

}

更新: 详细地说,这是我想做的真实的事情.与题名无关.

Update: In detail, this is real thing I want to do. Something is not related to question title.

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

    }
}

在API级别3中添加了

推荐答案

isPerformingCompletion().在从下拉列表中选择一个项目并返回 true 后,它会返回 true . TextWatcher 侦听器即将被触发.简而言之,为避免描述以下行为:

isPerformingCompletion() was added in API Level 3. It returns true after an item has been selected from the drop-down list and TextWatcher listeners are about to be triggered. In short, to avoid the behaviour described:

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
}

这篇关于如何避免同时被调用:AutoCompleteTextView上的onItemClicked和onTextChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆