edit view with icon drawable and Tag usage
2013-11-26 00:00
501 查看
Let's say you generate a bunch of views that are similar. You could set an OnClickListener for each view individually:
Then you have to create a unique onClick method for each view even if they do the similar things, like:
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:
Now we can use the same OnClickListener for every button:
Keep in mind that you can add any object as a tag. If the data you're adding to the view is dynamic, then the easiest thing to do would be to add a Hashtable to the View as the tag. Then add all the key/value pairs you want to that Hashtable. On the other end, (the code processing the tag), you can iterate over the values by casting the View's tag back into a Hashtable, and iterating over Hashtable.keys().
<EditText
android:id="@+id/projectCategorty2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:hint="@string/project_description"
android:drawableLeft="@drawable/icon_project_mobile"
android:drawableEnd="@drawable/icon_triangle"
android:editable="false"
/>
button1.setOnClickListener(new OnClickListener ... ); button2.setOnClickListener(new OnClickListener ... ); ...
Then you have to create a unique onClick method for each view even if they do the similar things, like:
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);
Now we can use the same OnClickListener for every button:
listener = new OnClickListener() { @Override public void onClick(View v) { doAction(v.getTag()); } };
Keep in mind that you can add any object as a tag. If the data you're adding to the view is dynamic, then the easiest thing to do would be to add a Hashtable to the View as the tag. Then add all the key/value pairs you want to that Hashtable. On the other end, (the code processing the tag), you can iterate over the values by casting the View's tag back into a Hashtable, and iterating over Hashtable.keys().
<EditText
android:id="@+id/projectCategorty2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:hint="@string/project_description"
android:drawableLeft="@drawable/icon_project_mobile"
android:drawableEnd="@drawable/icon_triangle"
android:editable="false"
/>
相关文章推荐
- This tag and its children can be replaced by one <TextView/> and a compound drawable
- android xml文件中出现如下提醒:This tag and its children can be replaced by one <TextView/> and a compound drawable
- This tag and its children can be replaced by one <TextView/> and a compound drawable
- 关于 This tag and its children can be replaced by one <TextView/> and a compound drawable 的问题
- This tag and its children can be replaced by one <TextView/> and a compound drawable
- Edit Control with Icon and Background Text
- This tag and its children can be replaced by one <TextView/> and a compound drawable
- This tag and its children can be replaced by one <TextView/> and a compound drawable
- Edit Control with Icon and Background Text
- eclipse提示:This tag and its children can be replaced by one <TextView/> and a compound drawable
- android This tag and its children can be replaced by one <TextView/> and a compound drawable 布局文件提示
- This tag and its children can be replaced by one <TextView/> and a compound drawable
- 【Android学习笔记】This tag and its children can be replaced by one <TextView/> and a compound drawable
- eclipse提示:This tag and its children can be replaced by one <TextView/> and a compound drawable
- Edit Control with Icon and Background Text
- Get installed Applications with Name, Package Name, Version and Icon
- How to Implement Add/Edit/Delete/View with PHP using Angular JS (Part-2)
- ListView解决图片错乱为什弱引用相关比findViewWithTag效率要高?
- uibutton *btn =(uibutton *)【self.view viewWithTag:10】;
- How to Implement Add/Edit/Delete/View with PHP using Angular JS (Part-1)