如何为 AutoCompleteTextView 设置 setOnClickListener?

发布于 2024-12-23 03:55:01 字数 542 浏览 4 评论 0原文

我正在为 AutoCompleteTextView 选择文本。我想将 setonclicklistener 应用于所选文本后。如果有任何想法。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

初心 2024-12-30 03:55:01

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

第一种方式是在layout xml中,你可以定义onCLick属性,用你想要调用的函数,在下面的例子中,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" />

然后,在您的活动中,您定义单击的功能。

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

或者您可以直接在代码中设置它:

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

如果您想在用户单击下拉列表中的项目时设置单击侦听器,则可以使用另一种方法,即 setOnItemClickListener

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

您还有最后一个选项,当用户实际使用 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

祝你好运!

There are different click listeners in AutoCompleteTextView.

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

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
    }
})

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
    }
})

References:

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

Good luck!

才能让你更想念 2024-12-30 03:55:01

您需要创建自定义适配器并将 OnClick 事件分配给 getView() 中的视图

You need to create Custom Adapter and assign OnClick event to the view in getView()

花辞树 2024-12-30 03:55:01

创建一个自定义适配器,并在该 getView() 方法中放置 setonclicklistenerforeach 视图并调用接口方法。

覆盖片段中的接口并将其传递给适配器,以便 onclick 您将在片段中获得点击。

interface IOnClick{
    fun onClick(model: Model)
}

override fun onClick(model: Model) {
        viewModel.setFormDataOnIdSelection(Model)
        binding.tietName.dismissDropDown()
    }

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
    newView.setOnClickListener {
        iOnClick.onClick(resAllLeadsModel)
    }
}

binding.tietName.setOnItemClickListener { parent, view, position, id ->
    viewModel.setFormDataOnIdSelection(listOfItems[view.tag.toString().toInt()])
}

这将在每种情况下按照您的意愿工作。

如果您有任何建议,请告诉我,谢谢。

Create a custom adapter and in that getView() method put setonclicklistenerforeach view and call interface method.

Override interface in fragment and pass that to adapter so onclick you'll get click in fragment.

interface IOnClick{
    fun onClick(model: Model)
}

override fun onClick(model: Model) {
        viewModel.setFormDataOnIdSelection(Model)
        binding.tietName.dismissDropDown()
    }

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
    newView.setOnClickListener {
        iOnClick.onClick(resAllLeadsModel)
    }
}

binding.tietName.setOnItemClickListener { parent, view, position, id ->
    viewModel.setFormDataOnIdSelection(listOfItems[view.tag.toString().toInt()])
}

this will work as you wanted in every scenario.

Please if you have any suggestion do let me know thanks.

妄断弥空 2024-12-30 03:55:01

这是一篇非常旧的帖子,所以我不确定这会有多大帮助,但是......现在在 Kotlin 中这真的很容易。我有一个文本自动完成功能,其中填充了字典中的值,并且访问名为 searchAutoComplete 的自动完成功能的下拉文本是...

        searchAutoComplete.setOnItemClickListener { 
           parent, view, position, rowId ->
            Log.d(TAG, "searchAutoComplete: setOnItemClickListener," +
                    "view -> ${view.javaClass.simpleName}, " +
                    "pos = $position. " +
                    "text = ${(view as TextView).text}")
        }

This is a really old post so I'm not sure how helpful this will be but... in Kotlin now this is really easy. I have a text auto-complete that is populated with with values from a dictionary and accessing the dropdown text for an auto-complete called searchAutoComplete is ...

        searchAutoComplete.setOnItemClickListener { 
           parent, view, position, rowId ->
            Log.d(TAG, "searchAutoComplete: setOnItemClickListener," +
                    "view -> ${view.javaClass.simpleName}, " +
                    "pos = $position. " +
                    "text = ${(view as TextView).text}")
        }
依 靠 2024-12-30 03:55:01

这对我来说非常有效

Actvl.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
    }
});

This works perfectly for me

Actvl.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
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文