星空网 > 软件开发 > 操作系统

What is the main purpose of setTag() getTag() methods of View?

如题:方法setTag(),getTag()在View中起到的作用

为什么我会找到这么一个问题,主要原因就是,我在学习ViewHolder时遇到,当时只知道怎么使用。在google时,发现stackoverflow上有不错的解释。遂翻译一二。

 

源地址:http://stackoverflow.com/questions/5291726/what-is-the-main-purpose-of-settag-gettag-methods-of-view

 

释疑:

比如说,你创建了很多个相似的View,你可以给你的View都各自添加一个OnClickLisener的属性,如下所示一样:

 

button1.setOnClickListener(new OnClickListener ... );button2.setOnClickListener(new OnClickListener ... ); ...

 

然后你需要为每一个View创建一个独一无二的Onclick方法,即使他们相应的事件都是一样的。就像如下。

public void onClick(View v) {  doAction(1); // 1 for button1, 2 for button2, etc.}

 

This is because onClick has only one parameter, a View, and it has to get information other information from instance variables or final local variables in enclosing scopes. What we really want is to get information from the views themselves.

 

enter getTag()/setTag();

button1.setTag(1);button2.setTag(2);

 

 

 现在我们可以给每一个button使用相同的OnClickListener:

listener = new OnClickListener() {  @Override  public void onClick(View v) {    doAction(v.getTag());  }};

 

 

 

It's basically a way for views to have memories.

这句话可以细细品味的。

 

目前尚未发现实际应用,但这篇文章主要因ViewHolder而起,恩对,就是ViewHolder中的setTag的应用。

想再了解的可以看看ViewHolder中的应用。 

 




原标题:What is the main purpose of setTag() getTag() methods of View?

关键词:ie

ie
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

数据资源管理新规来了,企业财税合规将面临新挑战!:https://www.kjdsnews.com/a/1602917.html
“兼职卖家”3大躺赢经验分享,让你轻松实现主业副业两手抓!:https://www.kjdsnews.com/a/1602918.html
短视频运营方案制作5步法:https://www.kjdsnews.com/a/1602919.html
美国贸易代表:抗议印度PC相关产品进口限制:https://www.kjdsnews.com/a/1602920.html
马士基升级调整这条印度航线!可直达那瓦西瓦港等:https://www.kjdsnews.com/a/1602921.html
需求疲软!MSC这条航线将停航6周:https://www.kjdsnews.com/a/1602922.html
北京到嵩山自驾游沿途景点 北京距离嵩山有多远:https://www.vstour.cn/a/411244.html
泰国签证有出生地 泰国出生入籍护照:https://www.vstour.cn/a/411245.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流