How to set multiple tags to a button?

后端 未结 4 1621
既然无缘
既然无缘 2021-02-07 07:12

I have 16 buttons and I tag them to pair some terms set to buttons and imported from sqlite database. So, I tag them like this:

// labelForButton and tagForButto         


        
相关标签:
4条回答
  • 2021-02-07 07:58

    If you need to add multiple tag into one view then you have to define the id for every tag in strings.xml file like:

    <item type="id" name="section" />
    <item type="id" name="hide_show" />
    

    After adding the key you can use these keys in java file like below:

    rowView.setTag(R.id.section,mSectionList.get(position));
    rowView.setTag(R.id.hide_show,"close");
    

    This will set the tag. At the time of getting tag you need to typecast the object which originally you set like:

    String mSection=(String)rowView.getTag(R.id.section);
    String isOpen=(String)rowView.getTag(R.id.hide_show);
    
    0 讨论(0)
  • 2021-02-07 08:06

    You should use the setTag(int key, Object tag) method, which also takes a second parameter key. This will allow you to set multiple tags on each Button like this :

    button.setTag(1,object1);
    button.setTag(2,object2);
    
    0 讨论(0)
  • 2021-02-07 08:08

    Define the key id in strings.xml and then get it through the id example:

    In String.xml declare the following code

    <item type="id" name="date" />
    
    <item type="id" name="name" />
    

    now set the tag like following

     share.setTag(R.id.date,it.adjournDate);
     share.setTag(R.id.name,it.partyName);
    

    Share is my button name

    Now finally get the tag in button where you want these values listener like this:

    v.getTag(R.id.name)
    v.getTag(R.id.date)
    
    0 讨论(0)
  • try :

    button.setTag(R.id.resource_id1,obj1);
    button.setTag(R.id.resource_id2,obj2);
    button.setTag(R.id.resource_id3,obj3);
    

    and to get the tags, use

    v.getTag(R.id.resource_id1);
    v.getTag(R.id.resource_id2);
    v.getTag(R.id.resource_id3);
    

    but, this will return only objects. you need to typecast it.

    0 讨论(0)
提交回复
热议问题