【问题标题】:Capturing TextChanged event inside a Listview in Android在 Android 的 Listview 中捕获 TextChanged 事件
【发布时间】:2012-02-05 11:52:39
【问题描述】:

我有一个包含 EditText 的 ListView。我想捕获这个文本框的 textchanged 事件并更新其他列。

在我的 customAdapter 中我做了这个

public View getView(int position, View inView, ViewGroup parent) {
    View v = inView;
    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.cartlistview, null);
    }

    EditText edQty = (EditText) v.findViewById(R.id.cartProductQuantity);
    edQty.addTextChangedListener(new QtyChangedWatcher(position, v));
}

即使用户没有更改任何内容,事件也会自行触发。此外,当用户仅更改第一行的输入时,会触发该事件,但会触发列表视图中的所有文本框。

我做错了吗?

谢谢

【问题讨论】:

  • 当您的Activity 创建时,是否有任何EditText 对象具有焦点?
  • 我不确定我所做的是否正确。在我的偶数代码中,我检查了之前和之后的值,如果它们不同,我继续我的代码。我在文本框中使用 setTag 函数存储了先前的值。

标签: android listview


【解决方案1】:

试试这个:

public View getView(int position, View inView, ViewGroup parent) {
    View v = inView;
    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.cartlistview, null);

    EditText edQty = (EditText) v.findViewById(R.id.cartProductQuantity);
    edQty.addTextChangedListener(new QtyChangedWatcher(position, v));
    }
}

(将 addTextChangedListener 移到 if 条件中)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    相关资源
    最近更新 更多