如何根据 Android 中的 AutoCompleteTextView 值设置 EditText 值?
How to set EditText value it depend on AutoCompleteTextView value in Android?
对话框中有一个AutoCompleteTextView、Edittext和Button。如果使用 AutoCompleteTextView 则关注下拉列表。所以它可以轻松 select。如果有人 select AAA 在 autocompletetextview 然后设置 edittext 值 BBB。例如。如果 select CCC 则将 editext 值设置为 DDD。 EditText 值将根据 AutoCompleteTextView selection 的值进行更改。如果 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);
}
使用这个
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
尝试使用以下代码:
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");}
}
});
}
对话框中有一个AutoCompleteTextView、Edittext和Button。如果使用 AutoCompleteTextView 则关注下拉列表。所以它可以轻松 select。如果有人 select AAA 在 autocompletetextview 然后设置 edittext 值 BBB。例如。如果 select CCC 则将 editext 值设置为 DDD。 EditText 值将根据 AutoCompleteTextView selection 的值进行更改。如果 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);
}
使用这个
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
尝试使用以下代码:
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");}
}
});
}