如何为 AutoCompleteTextView 设置 setOnClickListener? [英] how to set setOnClickListener for AutoCompleteTextView?

查看:34
本文介绍了如何为 AutoCompleteTextView 设置 setOnClickListener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 AutoCompleteTextView 选择文本.在我想将 setonclicklistener 应用于选定的文本之后.如果有任何想法.

I am selecting text for AutoCompleteTextView.After i want apply setonclicklistener to selected text.if any have idea.

ArrayAdapter<String> arrAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, sampleACTV); 
 AutoCompleteTextView ACTV = (AutoCompleteTextView) findViewById(R.id.spinner);
 ACTV.setAdapter(arrAdapter); 

 }
 private static final String[] sampleACTV = new String[]
         { "android","androidpeople.com","iphone","blackberry" }; 

在我的例子中,我选择了一个像 android 调用一个意图去嵌套 Acitivity

in my example i am selecting one like android call an intent to go to nest Acitivity

推荐答案

AutoCompleteTextView 中有不同的点击监听器.

There are different click listeners in AutoCompleteTextView.

第一种方式是在layout xml中,你可以定义onCLick属性,用你想要调用的函数,在下面的例子中,点击.

The first way is in the layout xml, you can define the onCLick attribute, with the function that you want to be called, in the example below, clicked.

<AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="clicked" />

然后,在您的活动中,您定义点击的函数.

Then, in your activity, you define the function clicked.

public void clicked(View v) { 
  // on click do ..
} 

或者你可以直接在你的代码中设置:

Or you can set it directly in your code:

ACTV.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        finish();
    }
});

如果您想在用户单击下拉列表中的项目时设置单击侦听器,还有另一种方法,setOnItemClickListener.

If you want to set the click listener when the user clicks in an item in the dropdown list there is another method, the setOnItemClickListener.

ACTV.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
        //... your stuff
    }
})

您还有最后一个选项,当用户使用 setOnItemSelectedListener 实际选择下拉列表中的项目时,设置点击侦听器.

And you have a last option, to set the click listener when the user actually selects an item in the dropdown list using setOnItemSelectedListener.

ACTV.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected (AdapterView<?> parent, View view, int position, long id) {
        //... your stuff
    }
    @Override
    public void onNothingSelected (AdapterView<?> parent) {
        //... your stuff
    }
})

参考资料:

http://developer.android.com/reference/android/widget/AutoCompleteTextView.html

祝你好运!

这篇关于如何为 AutoCompleteTextView 设置 setOnClickListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆