开发者

When do I need to use "android:id"?

开发者 https://www.devze.com 2023-01-07 07:36 出处:网络
I saw it used in textview and webview android:id=\"@+id/textview\" android:id=\"@+id/webview\" but looks like there i开发者_运维百科s none for DigitalClock widget.

I saw it used in textview and webview

android:id="@+id/textview"
android:id="@+id/webview"

but looks like there i开发者_运维百科s none for DigitalClock widget.

So, when do I have to use it? Just for text and web views?


No. That kind of identifiers are customizable by the programmer. It's used to reference the Views from the Java code. For instance:

TextView foo = (TextView) findViewById(R.id.textview);

Of course, you can use any name you want. For example:

android:id="@+id/whatever_you_want"

Will be referenced this way:

TextView foo = (TextView) findViewById(R.id.whatever_you_want);

Another thing to bear in mind is that there are some IDs that are reserved. Any way, you will recognize user-created IDs because they contain a plus (+): @+id/whatever


You use it when you need it, either to:

  • Reference it from Java (R.id.whatever)
  • Reference it from elsewhere in your layout (see RelativeLayout)
0

精彩评论

暂无评论...
验证码 换一张
取 消