zoukankan      html  css  js  c++  java
  • Android setTag()与getTag(),与set多个setTag()

    首先我们要知道setTag方法是干什么的,SDK解释为

    Tags

    Unlike IDs, tags are not used to identify views. Tags are essentially an extra piece of information that can be associated with a view. They are most often used as a convenience to store data related to views in the views themselves rather than by putting them in a separate structure.

     

    Tag不像ID是用标示view的。

    Tag从本质上来讲是就是相关联的view的额外的信息。

    它们经经常使用来存储一些view的数据。这样做很方便而不用存入另外的单独结构。

     Tag有一个特点,绑定数据到指定控件(view),而不显示出来。

    setTagandroidview类中非常实用的一个方法,

    与setId()不同。findViewById找到的是对象本身。

    而setTag(),取出来的是对象所指向的对象。



    在同一个类中,存取数据非常方便,不用数据库建库,

    也比sharePreference更节约内存。

    setTagObject tag)方法比較简单 


    比方

    TextView tvExecutor = (TextView)findViewById(R.id.t);

    tvExecutor.setTag(selectedUserMap);


    这个selectedUserMap 

    能够使Map<String, String> selectedUserMap这个对象。

    也能够是LinkedList<CarEntity> mList 这个对象。

           能够存储各种暂时数据,可是还不能称其为存储方式。

          在开发中,我们能够使用setTag(),getTag(),存取数据。

          可不能够一个对象。设置多个Tag呢?答案是肯定的

    
    
    在res/values/strings.xml中加入
    <span face="Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif" style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;"><span style="line-height: 18px;"><span style="color:#cc0000;"><resources></span></span></span>
    <span style="color:#cc0000;"><span face="Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif" style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;"><item type="id" name="tag_a</span><span style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; background-color: rgb(240, 240, 240);">"></item></span></span>
    <span face="Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif" style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;"><span style="color:#cc0000;"><item type="id" name="tag_b"></item></span></span>
    <span face="Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif" style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;"><span style="color:#cc0000;"></resources></span></span>
    <span face="Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif" style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;">
    </span>
    在activity中,使用的时候写成
    <span face="Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif" style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;"></span>
    <code style="border: 0px; vertical-align: baseline; background-color: rgb(238, 238, 238); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; padding: 0px; margin: 0px;"><span style="color:#cc0000;"><span class="pln" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;"><span style="font-family: Arial; font-size: 14px; line-height: 26px;">tvExecutor</span></span><span class="pun" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">.</span><span class="pln" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">setTag</span><span class="pun" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">(</span><span class="pln" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">R</span><span class="pun" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">.</span><span class="pun" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;"><span color="#00008b">id.tag_a</span></span><span class="pun" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">,</span><span class="pln" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;"> </span><span class="str" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">"逗比"</span><span class="pun" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">);</span><span class="pln" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">
    <span style="font-family: Arial; font-size: 14px; line-height: 26px;">tvExecutor</span></span><span class="pun" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">.</span><span class="pln" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">setTag</span><span class="pun" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">(</span><span class="pln" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">R</span><span class="pun" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">.</span><span class="pun" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;"><span color="#00008b">id.tag_b</span></span><span class="pun" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">,</span><span class="pln" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;"> </span><span class="str" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">"2货"</span><span class="pun" style="border: 0px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;">);</span></span></code>

    其有用的最多的是convertView中

    convertView中的TAG

    1.         对于使用了LayoutInflater对象进行View扩充的Tag的使用

             在之前。在adapter中,我们在getView中是这么些的代码:

    public View getView(int position, View convertView, ViewGroup parent) {

     

           ViewHolder holder = null;

           if (convertView == null) {

     

              holder = new ViewHolder();

     

              convertView = inflater.inflate(R.layout.vlist2null);

              holder.img = (ImageView) convertView.findViewById(R.id.img);

              holder.title = (TextView) convertView.findViewById(R.id.title);

               holder.info = (TextView)

    convertView.findViewById(R.id.info);

              // setTag的妙用

              convertView.setTag(holder);

     

           } else {

     

              holder = (ViewHolder) convertView.getTag();

           }

     

     ……略

    }

    样例3   在onClick点击事件中。使用tag

     View中的setTag(Onbect)表示给View加入一个格外的数据。以后能够用getTag()将这个数据取出来。

      能够用在多个Button加入一个监听器,每一个Button都设置不同的setTag。这个监听器就通过getTag来分辨是哪个Button 被按下。

      import android.app.Activity;

      import android.os.Bundle;

      import android.view.View;

      import android.widget.Button;

      public class Main extends Activity {

      @Override

      public void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.main);

      Button button1 = (Button) findViewById(R.id.Button01);

      Button button2 = (Button) findViewById(R.id.Button02);

      Button button3 = (Button) findViewById(R.id.Button03);

      Button button4 = (Button) findViewById(R.id.Button04);

      MyListener listener = new MyListener();

      button1.setTag(1);

      button1.setOnClickListener(listener);

      button2.setTag(2);

      button2.setOnClickListener(listener);

      button3.setTag(3);

      button3.setOnClickListener(listener);

      button4.setTag(4);

      button4.setOnClickListener(listener);

      }

      public class MyListener implements View.OnClickListener {

      @Override

      public void onClick(View v) {

      int tag = (Integer) v.getTag();

      switch (tag) {

      case 1:

      System.out.println("button1 click");

      break;

      case 2:

      System.out.println("button2 click");

      break;

      case 3:

      System.out.println("button3 click");

      break;

      case 4:

      System.out.println("button4 click");

      break;

      }

      }

      }

      }




  • 相关阅读:
    mysql重置id
    mysql数据类型
    手把手教你新建一个Vue项目
    用markdown开始优雅的写作
    源码阅读心得
    断点调试-程序员的必修课
    代码还是短点好!
    GoJS v1.8.27 去水印方法
    VS code不用集成终端如何修改并推送分支?
    LeetCode日拱一卒
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/6882871.html
Copyright © 2011-2022 走看看