我想实现的功能是当选择自动完成文本框的某一项时,添加的监听器就会有反应。用如下代码没有实现,请高人指点问题出现在哪里。程序中的PhoneName是自动完成文本框的名称。
PhoneName.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(check.this," selected", Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
}
我之前用这段代码实现了这个功能:
phoneName.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos,
long id) {
Toast.makeText(check.this," selected", Toast.LENGTH_LONG).show();
}
}
public class ActivityautoCompleteTextView extends Activity implements TextWatcher {
private the TextView selection;
private AutoCompleteTextView the edit;
private String [] items = {"lorem", "ipsum", "dolor", "sit", "amet", "consectetuer", "adipiscing", "elit", "morbi"," purus "};
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.ActivityautoCompleteTextView);
selection = (TextView) findViewById (R.id.selection6);
edit = (AutoCompleteTextView) findViewById (R.id. editAuto);
edit. setAdapter (new ArrayAdapter <String> (this, android.R.layout.simple_dropdown_item_1line, items));
edit. addTextChangedListener (this);
}
public void afterTextChanged (Editable s) {
}
public void beforeTextChanged (CharSequence s, int start, int count, int after) {
}
public void onTextChanged (CharSequence s, int start, int before, int count) {
selection.setText (edit.getText ());
}
}
使用OnItemClickListener,而不是OnItemSelectedListener应该是有作用的。