如何将监听器添加到autocompletetextview,android?

时间:2012-03-08 11:18:21

标签: android autocompletetextview

我正在尝试添加在autocompletetextview上选择项目时会做出反应的侦听器......任何人都可以提供帮助 // phonename是autocompletetextview

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) {
                // TODO Auto-generated method stub

            }
        });

3 个答案:

答案 0 :(得分:73)

试试这个:

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

        }
    });

答案 1 :(得分:1)

在科特林,这是:

autoCompleteTextView.setOnItemClickListener { _, _, position, _ -> 
    // You can get the label or item that the user clicked:
    val value = adapter.getItem(position) ?: ""
    Toast.makeText(this, value, Toast.LENGTH_LONG).show();
}

我还建议您以小写字母开头的变量命名,以免将变量与类型混淆。

答案 2 :(得分:0)

科特林:

    v.autoCompleteTextView.setOnItemClickListener { parent, view, position, id ->
        Toast.makeText(requireContext(),"$position selected",Toast.LENGTH_LONG).show()
    }