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

发布于 2024-11-29 18:11:08 字数 4535 浏览 1 评论 0原文

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

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

    }
}

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

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恍梦境° 2024-12-06 18:11:08

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
}

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
}
好倦 2024-12-06 18:11:08

这个问题没有特殊的解决方案。我建议您应该在 textchangedlistener() 的开头放置 onclicklistener() 内的代码,以便您想要首先执行的代码将首先执行,然后是你想要最后执行将最后执行。希望这对你有帮助。

There is no particular solution for this problem.I will suggest that you should put code on which is in inside onclicklistener() in the begining of textchangedlistener() so that the code that u want to get execute first will execute first then the code that you want to get execute last will execute last.Hope this will help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文