【问题标题】:Android afterTextChanged get EditText tagAndroid afterTextChanged 获取 EditText 标签
【发布时间】:2014-11-23 20:52:44
【问题描述】:

我有一个包含ListViewDialogFragment,并带有一个连接到ListView 的自定义适配器。该列表显示一堆项目,每条记录都有一个EditText,以允许用户输入数量。

当这些数量中的任何一个发生变化时,我需要在适配器中更新我的数组,这意味着将EditText 链接到数组中的特定元素。我使用EditText 的getTag / setTag 方法来做到这一点。数组中的项目通过两个属性是唯一的:

LocationIDRefCode

这些存储在我的TagData 对象中并在getView() 处设置。一旦值发生更改,我正在尝试使用EditText.getTag(),遗憾的是无济于事。

问题是我无法在afterTextChanged 方法中访问EditText

这是我的适配器的getView() 方法:

@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 对象并使用setTag() 将它绑定到EditText。我还在getView() 中连接了addTextChangedListener。其中afterTextChanged 方法如下所示:

@Override
public void afterTextChanged(Editable editable) {
    EditText editText = (EditText)context.getCurrentFocus(); // This returns the WRONG EditText!?

    // I need this 
    TagData locAndRefcode = (TagData) editText.getTag();
}

根据this 的帖子,Activity.getCurrentFocus() 应该返回有问题的EditText,它没有。相反,它会从 DialogFragment 后面的视图返回 EditText

这让我陷入困境。如何从我的 afterTextChanged 方法中访问 EditText 的标签?

【问题讨论】:

    标签: java android listview


    【解决方案1】:

    如果您将 txtQuantity 声明为 final,然后将匿名 new TextWatcher() { ... } 传递给 addTextChangedListener,那么您可以直接在 afterTextChanged(Editable s) 方法中使用 txtQuantity。 希望这会有所帮助。

    【讨论】:

    • 作为 Java 新手,我什至没有想到这种模式!完美解决了我的问题。谢谢+1
    【解决方案2】:

    你可以使用这个代码

    private Activity activity;
    private TextWatcher textWatcher = new TextWatcher() {
    
          @Override
          public void afterTextChanged(Editable s) {
              View focView=activity.getCurrentFocus();
              /* if t use EditText.settxt to change text  and the user has no 
               * CurrentFocus  the focView will be null
               */
              if(focView!=null)
              {
             EditText edit= (EditText) focView.findViewById(R.id.item_edit);
             if(edit!=null&&edit.getText().toString().equals(s.toString())){    
             edit.getTag() 
             }
            }
          }
    
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {
          }
    
          public void onTextChanged(CharSequence s, int start, int before,
                  int count) {
    
          }     
    
        };
    public EditAdapter(ArrayList<HashMap<String, String>> list, Activity activity){
        this.activity = activity;
        this.list = list;
        inflater = LayoutInflater.from(activity);
    }
    

    【讨论】:

    • 我已经使用上述@dev.bmax 的解决方案解决了这个问题,但这看起来也可以。感谢您的建议,我相信它可能对其他人有所帮助:)
    【解决方案3】:

    您可以使用EditText#getEditableText 方法:

    @Override
    public void afterTextChanged(Editable s) {
    
        if(editText.getEditableText() == s){
            //
            // Your code
            //
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-12
      • 1970-01-01
      相关资源
      最近更新 更多