【问题标题】:setTag() on button click for multiple valuesetTag() on button click for multiple value
【发布时间】:2014-05-27 04:59:58
【问题描述】:
        dater.setText(theday);
        dater.setTag(1, theday);
        dater.setTag(2, theday + "-" + themonth + "-" + theyear);

        dater.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                notification(date_txt);
                String day = v.getTag(1).toString();
                date_txt.setText(day);
            }
        });

我正在为一个 textview 设置标签我正在使用 settag(int key object tag);和 settag(key) 但我遇到了错误,我对此没有任何想法,谢谢

【问题讨论】:

  • 我犯了一个错误,我在帖子中读到密钥应该在资源中或类似的地方,所以我所做的就是尝试将密钥更改为类似 R.id 的东西。日期和它的工作我会尽量得到整个日期我希望我还能工作..lol

标签: android tags


【解决方案1】:
dater.setText(theday);
   List<String> data = new ArrayList<String>();
   data.add(theday);
   data.add(theday + "-" + themonth + "-" + theyear);
    dater.setTag(data);


    dater.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            notification(date_txt);
            ViewHold holder=v.getTag
            String day = ((List<String>)v.getTag()).get(1).toString();
            date_txt.setText(day);
        }
    });

【讨论】:

    【解决方案2】:

    这个答案很有用

    textView.setTag(new HoldsTwoObjs(obj1, obj2));
    HoldsTwoObjs isTagClass=(HoldsTwoObjs)textView.getTag();
    

    https://stackoverflow.com/a/27528427/1140304

    【讨论】:

      【解决方案3】:

      您在settag(int key object tag); 中使用的密钥应该是在您的资源中声明的id

      根据文档

      Sets a tag associated with this view and a key. A tag can be used to mark a view in its
      hierarchy and does not have to be unique within the hierarchy. Tags can also be used to
      store data within a view without resorting to another data structure. **The specified key 
      should be an id declared in the resources of the application to ensure it is unique (see 
      the ID resource type). Keys identified as belonging to the Android framework or not 
      associated with any package will cause an IllegalArgumentException to be thrown.**
      

      【讨论】:

      • 这就是我所做的,正如我在上面的评论中所说的那样,你有点晚了,谢谢你的努力
      猜你喜欢
      • 2017-06-18
      • 1970-01-01
      • 1970-01-01
      • 2011-11-02
      • 1970-01-01
      • 2022-12-02
      • 2020-04-19
      • 1970-01-01
      • 2012-10-06
      相关资源
      最近更新 更多