R.id值范围 [英] Scope of R.id values

查看:214
本文介绍了R.id值范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找方法,以减少什么,我会在我的应用程序调用ID污染。从我所知道的, R.id 是全球性的应用程序,所以在每一次活动我通常创建独特的资源ID值的元素,即使我有许多相同的事情。举例来说,如果我有三个活动课,每一个保存和取消按钮,我会定义六个独特的ID的 R.id 这样的:

I'm looking for ways to reduce what I would call "id pollution" in my apps. From what I can tell, R.id is global to the application, so in every Activity I usually create unique resource id values for elements, even if I have many of the same thing. For example, if I have three Activity classes, each with a save and cancel button, I would define six unique id's for R.id like:

R.id.actOne_save
R.id.actOne_cancel
R.id.actTwo_save
R.id.actTwo_cancel
R.id.actThree_save
R.id.actThree_cancel

这似乎没有必要对我来说,因为我真的应该只需要在任何活动两班倒。什么是一些你们都生成资源ID时使用的做法?你activites之间重用呢?那是确定的,如果一个ID在同一时间存在于两个Activites(也许有暂停和一个前台)?我怕怪异的行为就像一个按钮,点击打太多的听众!

This seems unnecessary to me, as I should really only need two running on any Activity. What are some of the practices that you all use when generating resource ids? Do you reuse them between activites? Is that OK if an id exists on two Activites (maybe one paused and one foreground) at the same time? I'm afraid of weird behavior like a button click hitting too many listeners!

推荐答案

您没事使用相同的ID在多个元素,只要它们不在同一个视图。所以,所有的保存按钮可以有 btn_save 的id,只要不在相同的布局文件中的两个人,或附着的布局,那么你罚款。

You're fine to use the same id across multiple elements, as long as they aren't in the same view. So, all your save buttons could have the id of btn_save and as long as there aren't two of them in the same layout file, or attached layouts, then you're fine.

这篇关于R.id值范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆