onItemClickListener用于多个AutoCompleteTextView

时间:2016-02-25 05:42:50

标签: android

我有三个AutoCompleteTextView并设置onItemClickListener,请你帮我知道哪个AutoCompleteTextView被点击了

false

3 个答案:

答案 0 :(得分:3)

使用View比较视图ID的onItemClick参数进行检查。如下所示

if(view.getId() == R.id.editTextUserNamecheck){

 }else if(view.getId() == R.id.editTextPhoneNoNew){

 }else if(view.getId() == R.id.searchProduct){
 }

public void onItemClick(AdapterView<?> parent, View view, int position, long id)

中使用上面的代码

答案 1 :(得分:2)

在onItemClick中

,您可以识别Adapter AdapterView父级,这样您就可以首先识别父适配器。

如下所示

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    if (parent.getAdapter().getClass().equals(ProductAutoCmplt .class)) {
      //do your stuff
    }
    else if(parent.getAdapter().getClass().equals(CustomPhoneArrayAdapterAutoCmplt .class)){ 
//do your stuff
}else if(parent.getAdapter().getClass().equals(CustomArrayAdapterAutoCmplt.class)){
//do your stuff
}
} 

答案 2 :(得分:0)

使用匿名内部类,我们可以这样做:

txtUserName.setOnItemClickListener((new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            // do your stuff;
        }
    });                              

txtContactNo.setOnItemClickListener((new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            // do your stuff;
        }
    });                          

txtSearchProduct.setOnItemClickListener((new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            // do your stuff;
        }
    });