如何设置setOnClickListener为AutoCompleteTextView?如何设置、setOnClickListener、AutoCompleteTextView

2023-09-06 02:00:25 作者:钢铁男神^

我的AutoCompleteTextView.After选择文本,我想申请setonclicklistener到text.if任何选择都有想法。

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" }; 

在我的例子中,我选择一个像机器人打电话的意图去鸟巢活性的研究

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.

第一种方法是在布局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 ..
} 

或者你也可以在你的code直接设置它:

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.setOnSelectedListener(new OnItemClickListener() {
    @Override
    public void onItemSelected (AdapterView<?> parent, View view, int position, long id) {
        //... your stuff
    }
})

参考文献:

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

祝你好运!