Mam ten kod. Kiedy wybieram element z listy sugestii, onTextChanged dzieje się najpierw, a następnie oItemClicked przychodzi po tym. Teraz, gdy chcę wybrać słowo, najpierw pojawia się "onItemClicked", potem "onTextChanged". Zrobiłem dokumentację dla systemu Android, ale nie wspomniałem o tym temacie.Jak uniknąć wywoływania obu nazw: onItemClicked i onTextChanged na AutoCompleteTextView
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) {
}
}
Aktualizacja: W szczegółach, to jest prawdziwe, co chcę robić. Coś nie jest związane z tytułem pytania.
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) {
}
}
czy sprawdziłeś, czy 'beforeTextChanged()' jest wywołane.? – Ronnie
Dlaczego nie można zrobić wszystkie rzeczy w OnclickListener()? – Ronnie
Tak, zrobiłem. Wszystkie funkcje TextWatchera występują przed onItemClick. – Emerald214