对话框中有一个AutoCompleteTextView,Edittext和Button。如果使用AutoCompleteTextView专注于下拉列表。所以它可以轻松选择。如果有人在autocompletetextview中选择 AAA ,请将edittext值设置为 BBB 。例如。如果选择 CCC ,请将editext值设置为 DDD 。 EditText值将由AutoCompleteTextView选择的值更改。我如何设置AutoComplete是 AAA 然后将值设置为Edittext BBB 。
请参阅下面的代码:
private EditText editText;
private AutoCompleteTextView completeTextView;
String[] items = { "AAA","CCC","EEE", "GGG"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//AuutoCompleteTextView
completeTextView = (AutoCompleteTextView) findViewById(R.id.complete_text);
// show list on focus
completeTextView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
completeTextView.showDropDown();
return false;
}
});
completeTextView.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (completeTextView.toString().equals("AAA")) {
editText.setText("BBB",TextView.BufferType.NORMAL);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
completeTextView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, items));
editText = (EditText) findViewById(R.id.edit_text);
}
答案 0 :(得分:1)
尝试使用以下代码:
private EditText editText;
private AutoCompleteTextView completeTextView;
String[] items = { "AAA","CCC","EEE", "GGG"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//AuutoCompleteTextView
completeTextView = (AutoCompleteTextView) findViewById(R.id.complete_text);
completeTextView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, items));
editText = (EditText) findViewById(R.id.edit_text);
completeTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(items[position].equals("AAA"){
editText.setText("BBB");}
}
});
}
答案 1 :(得分:0)
使用此
actv.setOnSelectedListener(new OnItemClickListener() {
@Override
public void onItemSelected (AdapterView<?> parent, View view, int position, long id) {
//... your stuff
}
})
参考文献:
http://developer.android.com/reference/android/widget/AutoCompleteTextView.html