gpt4 book ai didi

android - 如何在Android中获取触发事件的 View ?

转载 作者:行者123 更新时间:2023-11-29 21:58:47 25 4
gpt4 key购买 nike

你知道我怎样才能得到触发事件的 View 吗? 例如:

    final AutoCompleteTextView edtxInput = (AutoCompleteTextView)layout.findViewById(R.id.edtx_input);  
edtxInput.setThreshold(2);
edtxInput.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View arg1, int position, long arg3) {
Console.debug(TAG, "view: " + arg1, Console.Liviu);
edtxInput.setText(((FormOption)edtxInput.getAdapter().getItem(position)).getDescription());
}
});

这里的问题是我不知道如何在 OnItemClickListener 中更新来自 edtxInput 的文本而不使其成为最终文本。

谢谢

最佳答案

检查这个http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html

Parameters

parent The AdapterView where the click happened.

arg1 是 AutoCompleteTextView (edtxInput) 本身。所以你可以做

 AutoCompleteTextView edtxInput = (AutoCompleteTextView)layout.findViewById(R.id.edtx_input);  
edtxInput.setThreshold(2);
edtxInput.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View arg1, int position, long arg3) {
Console.debug(TAG, "view: " + arg1, Console.Liviu);
AutoCompleteTextView medtxInput = (AutoCompleteTextView)parent;
medtxInput.setText(((FormOption)medtxInput.getAdapter().getItem(position)).getDescription());
}
});

更新

似乎 AutoCompleteTextView 不是 AdapterView 的子类。这在某种程度上是 SDK 的“错误”。要获得 AutoCompleteTextView,您可以做一些 hack

 AutoCompleteTextView medtxInput = (AutoCompleteTextView)view.getParent();

所以你得到点击的 View ,然后得到它的父 View ,即 AutoCompleteTextView

但是为什么您不希望 AutoCompleteTextView 一开始就是最终的呢?有什么特别的原因吗?

如果你只想要适配器,你可以只写这个

((ChildClass)parent.getItemAtPosition(position)).getDescription();

关于android - 如何在Android中获取触发事件的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12490435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com