2 csh 34 csh_34 于 2012.10.12 16:06 提问

自动完成文本框添加监听器

我想实现的功能是当选择自动完成文本框的某一项时,添加的监听器就会有反应。用如下代码没有实现,请高人指点问题出现在哪里。程序中的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) {

            }
        }

3个回答

hxn_217
hxn_217   2012.10.12 17:50
已采纳

我之前用这段代码实现了这个功能:

 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();

            }
        }
hcg_21
hcg_21   2012.10.12 17:38
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 ());
}
}
ueryueryuery
ueryueryuery   2012.10.15 17:49

使用OnItemClickListener,而不是OnItemSelectedListener应该是有作用的。

csh_34
csh_34 嗯,你说的答案和hxn_217的如同一辙,也能添加代码的话就一目了然了。
大约 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片