android tag 筛选,Android 不得不知Tag的妙用

setTag () 是 Android 的 View 類中很有用的一個方法,可以用它來給控件附加一些信息,在很多場合下都得到妙用。我們可以看到 setTat() 有兩個方法重載,setTag(Object object) 和 setTag(int key,Object object)參數類型 都帶有 Object 也就是 可以保存任何 對象數據。

下面分別介紹下相關使用方法。

void setTag(Object tag)

這個方法相對簡單,如果只需要設置一個 tag,那么直接調用 setTag(Object tag) 取值:view.getTag();方法就可以輕松搞定。

void setTag (int key, Object tag)

官方的api文檔中提到:

“ 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 IllegalArgumentExceptionto be thrown.”

所以拋出 IllegalArgumentException 的原因就在於 key 不唯一,那么如何保證這種唯一性呢?很明顯定義一個 final 類型的 int 變量和硬編碼一個值的方式都是行不通的。比如下面一個錯誤的例子:private static final int TAG_ONLINE_ID = 1;

((Button)row.findViewById(R.id.btnPickContact)).setTag(TAG_ONLINE_ID,objContact.onlineid);05-18 20:29:38.044: ERROR/AndroidRuntime(5453): java.lang.IllegalArgumentException: The key must be an application-specific resource id.

05-18 20:29:38.044: ERROR/AndroidRuntime(5453):

at android.view.View.setTag(View.java:7704)

05-18 20:29:38.044: ERROR/AndroidRuntime(5453):

at com.mypkg.viewP.inflateRow(viewP.java:518)

那如果一定需要使用多個 tag 綁定怎么做呢?

那么這么做,在res/values/strings.xml中添加

使用imageView.setTag(R.id.tag_first, "Hello");

imageView.setTag(R.id.tag_second, "Success");

就這就保證了 key 值的唯一性。

取值String tag_first=v.getTag(R.id.tag_first).tostring();

就能取到值了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Tag的使用 package com.yarin.android.qiehuan; import android.app.AlertDialog; import android.app.Dialog; import android.app.TabActivity; import android.content.DialogInterface; import android.graphics.Color; import android.os.Bundle; import android.widget.TabHost; import android.widget.TabHost.OnTabChangeListener; public class Activity01 extends TabActivity { //声明TabHost对象 TabHost mTabHost; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //取得TabHost对象 mTabHost = getTabHost(); /* 为TabHost添加标签 */ //新建一个newTabSpec(newTabSpec) //设置其标签和图标(setIndicator) //设置内容(setContent) mTabHost.addTab(mTabHost.newTabSpec("test1") .setIndicator("TAB 1",getResources().getDrawable(R.drawable.img1)) .setContent(R.id.textview1)); mTabHost.addTab(mTabHost.newTabSpec("test2") .setIndicator("TAB 2",getResources().getDrawable(R.drawable.img2)) .setContent(R.id.textview2)); mTabHost.addTab(mTabHost.newTabSpec("test3") .setIndicator("TAB 3",getResources().getDrawable(R.drawable.img3)) .setContent(R.id.textview3)); //设置TabHost的背景颜色 mTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150)); //设置TabHost的背景图片资源 //mTabHost.setBackgroundResource(R.drawable.bg0); //设置当前显示哪一个标签 mTabHost.setCurrentTab(0); //标签切换事件处理,setOnTabChangedListener mTabHost.setOnTabChangedListener(new OnTabChangeListener() { // TODO Auto-generated method stub @Override public void onTabChanged(String tabId) { Dialog dialog = new AlertDialog.Builder(Activity01.this) .setTitle("善谢谢提醒") .setMessage("现在选中了:"+tabId+"标签") .setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { dialog.cancel(); } }).create();//创建按钮 dialog.show(); } }); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值