在Android中,id是用于标识UI元素的属性,可以填入任意合法的字符串作为id的值。通常情况下,可以使用小写字母、数字、下划线和点的组合作为id的值,以方便代码的阅读和维护。在布局文件中和代码中,通过id来引用UI元素。例如,在布局文件中可以使用以下方式给一个按钮设置id:
```
android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="My Button" /> ``` 在代码中可以通过以下方式获取对应的按钮: ``` Button button = findViewById(R.id.myButton); ```
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Button" />
在代码中可以通过以下方式获取对应的按钮:
Button button = findViewById(R.id.myButton);
查看详情