java 如何为一个按钮设置多个标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15911620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:16:17  来源:igfitidea点击:

How to set multiple tags to a button?

javaandroid

提问by marjanbaz

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:

我有 16 个按钮,我标记它们以配对设置为按钮并从 sqlite 数据库导入的一些术语。所以,我这样标记它们:

// labelForButton and tagForButton
       class MyStruct {
       public MyStruct (String lab, String t){
        label = lab;
        tag = t;
    }
       private String label;
       private String tag;
       }

mDbHelper.open();

            Cursor c = mDbHelper.getSpojnice(generateWhereClause());

            ArrayList<MyStruct> labelsA = new ArrayList<MyStruct>();
            ArrayList<MyStruct> labelsB = new ArrayList<MyStruct>();

            labelsA.add(new MyStruct(c.getString(2), "1")); // this tag should be the same to button that matches
            labelsB.add(new MyStruct(c.getString(3), "1"));
            labelsA.add(new MyStruct(c.getString(4), "2"));
            labelsB.add(new MyStruct(c.getString(5), "2"));
            labelsA.add(new MyStruct(c.getString(6), "3"));
            labelsB.add(new MyStruct(c.getString(7), "3"));
            labelsA.add(new MyStruct(c.getString(8), "4"));
            labelsB.add(new MyStruct(c.getString(9), "4"));
            labelsA.add(new MyStruct(c.getString(10), "5"));
            labelsB.add(new MyStruct(c.getString(11), "5"));
            labelsA.add(new MyStruct(c.getString(12), "6"));
            labelsB.add(new MyStruct(c.getString(13), "6"));
            labelsA.add(new MyStruct(c.getString(14), "7"));
            labelsB.add(new MyStruct(c.getString(15), "7"));
            labelsA.add(new MyStruct(c.getString(16), "8"));
            labelsB.add(new MyStruct(c.getString(17), "8"));

            Collections.shuffle(labelsA);
            Collections.shuffle(labelsB);

            a1.setText(labelsA.get(0).label);
            a1.setTag(labelsA.get(0).tag);
            a1.setOnClickListener(clickListener);
            b1.setText(labelsB.get(0).label);
            b1.setTag(labelsB.get(0).tag);
            b1.setOnClickListener(clickListener);
            a2.setText(labelsA.get(1).label);
            a2.setTag(labelsA.get(1).tag);
            a2.setOnClickListener(clickListener);
            b2.setText(labelsB.get(1).label);
            b2.setTag(labelsB.get(1).tag);
            b2.setOnClickListener(clickListener);
.
.
.
.
}
}

So, I need A1 and B1 to have same tags, also A2 and B2 and so on.

所以,我需要 A1 和 B1 具有相同的标签,还有 A2 和 B2 等等。

BUT, I also need for some other reason to all A buttons have tag for example "A" and all the B's "B". So, how to set multiple, in my case two, tags to one button?

但是,出于某些其他原因,我还需要所有 A 按钮都有标签,例如“A”和所有 B 的“B”。那么,如何将多个(在我的情况下为两个)标签设置为一个按钮?

EDIT:

编辑:

I added this to my string.xml:

我将此添加到我的 string.xml 中:

<item type="id" name="KOLONA1"/>
<item type="id" name="KOLONA2"/>

Then added tags:

然后添加标签:

a1.setTag(R.id.KOLONA1, labelsA.get(0).tag);
b1.setTag(R.id.KOLONA2, labelsA.get(0).tag);

回答by Arun kumar

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

如果您需要将多个标签添加到一个视图中,那么您必须为strings.xml文件中的每个标签定义 id,例如:

<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:

添加密钥后,您可以在 java 文件中使用这些密钥,如下所示:

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);

回答by Ovidiu Latcu

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

您应该使用该setTag(int key, Object tag)方法,该方法还带有第二个参数key。这将允许您像这样tags在每个上设置多个Button

button.setTag(1,object1);
button.setTag(2,object2);

回答by M.Prabha karan

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.

但是,这将只返回对象。你需要对它进行类型转换。

回答by Asif Moin Qureshi

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

在里面定义key idstrings.xml然后通过id例子获取:

In String.xml declare the following code

在 String.xml 中声明以下代码

<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)