Android的afterTextChanged获得标签的EditText [英] Android afterTextChanged get EditText tag
问题描述
我有一个包含 DialogFragment
A 的ListView
,具有自定义适配器挂接到的ListView
。该名单显示一个带一堆物品的EditText
每个记录允许用户输入量。
I have a DialogFragment
containing a ListView
, with a custom adapter hooked up to the ListView
. The list displays a bunch of items with an EditText
for each record to allow the user to enter a quantity.
在任何这些量的改变,我需要更新适配器内我的数组,这意味着在关联的EditText
数组中的特定元素。我做这个用的的EditText
的getTag / setTag方法。数组中的项是唯一由两个属性:
When any of these quantities change I need to update my array within the adapter, which means linking an EditText
to a specific element in the array. I do this using the getTag / setTag methods of the EditText
. Items in the array are unique by two properties:
LocationID
和参考code
这些都存储在我的 tagdata进行
对象,并设置在getView点()。我试图用 EditText.getTag()
一旦一个数值已经改变了,可悲的是无济于事。
These are stored in my TagData
object and set at the point of getView(). I'm attempting to use EditText.getTag()
once a value has changed, sadly to no avail.
问题是我无法访问的EditText
在 afterTextChanged
方法。
The problem is I can't access the EditText
in the afterTextChanged
method.
下面是我的适配器 getView()
方法:
Here's the getView()
method of my Adapter:
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ItemModel item = (ItemModel) getItem(i);
TagData tagData = new TagData();
tagData.setLocationID(item.getLocationID());
tagData.setRefCode(item.getRefCode());
EditText txtQuantity = ((EditText) view.findViewById(R.id.txtQuantity));
txtQuantity.setTag(tagData);
txtQuantity.setText(String.valueOf(item.getQtySelected()));
txtQuantity.addTextChangedListener(this);
...
return view;
}
我在上面创建一个 tagdata进行
对象,并将其绑到的EditText
使用 setTag( )
。我也勾了一个 addTextChangedListener
在 getView()
。对于在 afterTextChanged
方法是这样的:
Above I create a TagData
object and tie it to the EditText
using setTag()
. I also hook up an addTextChangedListener
in the getView()
. For which the afterTextChanged
method looks like this:
@Override
public void afterTextChanged(Editable editable) {
EditText editText = (EditText)context.getCurrentFocus(); // This returns the WRONG EditText!?
// I need this
TagData locAndRefcode = (TagData) editText.getTag();
}
根据这个后, Activity.getCurrentFocus()
应该有问题返回的EditText
,它没有。相反,它返回一个的EditText
从DialogFragment背后的视图。
According to this post, Activity.getCurrentFocus()
should return the EditText
in question, it doesn't. Instead it returns an EditText
from the View behind the DialogFragment.
这让我卡住了。我怎样才能从我的afterTextChanged法里访问一个的EditText
的标签?
Which leaves me stuck. How can I get access to an EditText
's tag from within my afterTextChanged method?
推荐答案
如果您将宣布你txtQuantity作为最后再通过一个匿名新TextWatcher(){...}进入addTextChangedListener,那么你可以直接使用里面txtQuantity在afterTextChanged(编辑S)的方法。
希望这有助于。
If you would declare your txtQuantity as final and then pass an anonymous new TextWatcher() { ... } into the addTextChangedListener, then you could directly use txtQuantity inside the afterTextChanged(Editable s) method. Hope this helps.
这篇关于Android的afterTextChanged获得标签的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!