难道Android的视图ID应该是独一无二的? [英] Are Android View id supposed to be unique?

查看:144
本文介绍了难道Android的视图ID应该是独一无二的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我很困惑的东西是否是Android的IDS必须是唯一的,或者不是。这是为什么产生了困惑:
远的不说有一个活动有一个的TextView (机器人:ID =文本)和按钮(机器人:ID =按钮)。该按钮设置文本视图的一个随机文本。因此,要增加一个听众,我将获取的按钮为
按钮B =(按钮)findViewById(R.id.button)然后添加听众做任务。
现在,我可以参考这个同一的TextView从 DialogFragment ,一个片段什么不能用相同的ID R.id.text 键,所有的改变都不会被应用到该文本视图毫无疑问。

Ok so something that I am confused with is whether Android ids need to be unique or not. Here is why the confusion arises:
Let's just say there is an Activity that has an TextView (android:id="text") and a Button (android:id="button"). The Button sets the text of the text view to a random text. So, to add a listener, I will retrieve the button as
Button b = (Button) findViewById(R.id.button) and then add listener to do the task.
Now, I can refer to this same TextView from a DialogFragment, a Fragment and what not with the same id R.id.text and all the changes will ever be applied to this text view without doubt.

在情况下的子类化 BaseAdapter ,你需要重写 getView ,你做的膨胀,如果有必要,如果不,你做的改变。现在,您检索浏览在几乎相同的方式。差不多。
你做了 convertView.findViewById(..)

In case of subclassing BaseAdapter, you need to override the getView where you do the inflating if necessary and if not, you make changes. Now, you retrieve the Views in almost the same way. Almost.
You do a convertView.findViewById(..).

如果所有的观点需要有一个唯一的ID,如何改变查看中的含量 getView 不会导致随意行为?
我的意思是,所有虚增的看法有相同的ID在布局中定义。

If all the views need to have a unique id, how does changing the content of View in the getView not result in haphazard behavior ?
I mean, all the views inflated have the same id as defined in layout.

我的理解是,ID是在夸大他们的观点的背景是独一无二的。因为我是一个初学者,我要求澄清

那么,这是什么意思是,我不能夸大两个片段活动通过使用相同的布局。这将导致一个异常。两者都是由同一个活动虚增,因此属于同一个实例。该ID将发生冲突

So, what this means is I can not inflate two Fragment in an Activity by using the same layout. This will result in an exception. Both are inflated by the same Activity, hence belong to the same instance. The ids will conflict

推荐答案

文档你可以阅读

的ID不一定是唯一的在整个树的,但它应该是独特的您正在寻找树的一部分(这往往是整个树中,所以最好是完全独特如果可能)。

An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching (which may often be the entire tree, so it's best to be completely unique when possible).

这意味着将不会有例外,如果你用相同的ID为您的所有意见,但很明显的布局将得到无用的话。

It means there will be no exceptions if You use same id for all Your views but obviously layout will get useless then.

FindViewById只是通过遍历树,直到找到与搜索ID第一个元素,并返回它(或NULL如果没有找到)的作品。如果你曾与相同的ID在树上几个要素你总是会得到相同的元素,一来就是先在树上。

FindViewById works simply by traversing a tree until it finds first element with searched id and returns it (or null if doesn't find). If You had few elements with same id in tree You will always get same element, the one that is first in tree.

您可能有大量的碎片充有相同的布局,就像你的ListView具有相同布局的每一个元素,那是因为充气不关心ID值。 它只是读取XML文件,并创建正确看待对象树仅此而已。

You may have plenty of fragments inflated with same layout just like you have ListView with each element having same layout, that is because inflater doesn't care about id values. It simply reads XML file and create a tree with correct view objects nothing more.

这篇关于难道Android的视图ID应该是独一无二的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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