在Android中定义相同的ID是否可以?

huangapple go评论127阅读模式
英文:

Is it okay to define same ids in Android?

问题

我知道如果我们定义不同的标识符,将来很容易找到它。我找到了许多答案,但它们不是我在寻找的。我有一个有点长的 Java 函数,将在许多 Java 活动中使用。因此,我不可能为每个 Java 和 XML 活动更改标识符。那么在不同的 XML 和 JAVA 文件中使用相同的标识符是否可以?或者它们会以任何方式导致应用程序崩溃?示例标识符:

XML 活动一:

<TextView  android:id="@+id/resultview"  />

JAVA 活动一:

printresult = (TextView) findViewById(R.id.resultview);

XML 活动二:

<TextView  android:id="@+id/resultview"  />

JAVA 活动二:

printresult = (TextView) findViewById(R.id.resultview);

如您所见,上述代码中有不同的 XML 和 JAVA 文件,但标识符是相同的。

英文:

I know that if we define different ids it is easy for us to find it in future. I found many answers but they're not what i'm looking for. I've a java function which is bit lengthy and will be used in many java activities. So it won't be possible for me to change ids for every java and xml activity. So is it okay to have same ids in different XML and JAVA files? Or could they make app crash in any way? IDs example :

XML ACTIVITY ONE :

&lt;TextView  android:id=&quot;@+id/resultview&quot;  /&gt;

JAVA ACTIVITY ONE :

printresult = (TextView) findViewById(R.id.resultview);

XML ACTIVITY TWO :

&lt;TextView  android:id=&quot;@+id/resultview&quot;  /&gt;

JAVA ACTIVITY TWO :

printresult = (TextView) findViewById(R.id.resultview);

As you can see above there are different XML and JAVA but ids are same.

答案1

得分: 0

你不会有问题,因为这些组件在不同的 XML 中。但最好为它们使用不同的名称,这样你自己会更舒适。例如:

<TextView  android:id="@+id/resultview1"  />
<TextView  android:id="@+id/resultview2"  />
英文:

You will not have problem because the components are in different XMLs. But it's better to put different names for them, you will be more comfortable yourself. For example:

&lt;TextView  android:id=&quot;@+id/resultview1&quot;  /&gt;
&lt;TextView  android:id=&quot;@+id/resultview2&quot;  /&gt;

答案2

得分: 0

可以在不同的XML文件中使用相同的id,但需要考虑的是,Activity会填充特定的布局,findViewById方法会在你填充的特定布局中使用id来查找视图。是的,这样做不会引入错误,但这是一种不好的做法。

你可以通过Android Studio的重构功能来重新命名id。所以,如果你在这里改变了id,Android Studio会搜索该特定id的用法,并为你进行更改。

  1. 右键单击要更改的id。

  2. 点击重构。

  3. 点击重命名。

  4. 你会看到出现以下对话框:

  5. 然后你可以通过更改作用范围和其他设置来调整重构的方式,使其更符合你的需求。

英文:

Yes it is okay to use same id in different in different XML files. The point to be considered here is that the activity inflates the particular layout and the findViewById method finds the view using the id in that particular layout that you inflated. Yes, this doesn't introduce bugs however it is a bad practice.

You can refactor the id's via android studio's refactor function. So if you change your id here android studio will search the usages of that particular id and change them for you. 在Android中定义相同的ID是否可以?

  1. Right click on the id you want to change.
  2. Click on refactor
  3. Click on rename
  4. You will see the following dialog appear

在Android中定义相同的ID是否可以?

  1. Then you can change the way of refactor by changing the scope and other things at your comfort.

答案3

得分: 0

没有问题将一个ID赋予多个视图,但是:

  1. 这些视图应该在不同的布局文件中。
  2. 这不是一个好的做法。
英文:

there is no problem to give one id to many views but:

  1. the views should be in different layout files
  2. it is not a good practice

答案4

得分: -1

没有问题,直到相同的ID在同一个文件中被使用。但这并不是一个好的做法。

英文:

There is no problem for duplicated IDs until they used in same file. But it is NOT a good practice.

huangapple
  • 本文由 发表于 2020年7月26日 14:12:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63096716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定