📅  最后修改于: 2023-12-03 15:31:28.466000             🧑  作者: Mango
When developing Android applications in Java, it's crucial to be able to manipulate the elements on the screen. In order to do this, you need to be able to get the element by its unique ID.
The following code shows an example of how to find an element in the layout XML file by its ID.
TextView myTextView = findViewById(R.id.my_text_view);
In this example, we are finding a TextView element with the ID "my_text_view". The findViewById
method returns a reference to the element, which we then store in the myTextView
variable.
Once you have the element reference, you can manipulate it in various ways. For example, you can change the text of a TextView element with the following code.
myTextView.setText("Hello, World!");
Or you can change the background color of a View element with the following code.
View myView = findViewById(R.id.my_view);
myView.setBackgroundColor(Color.BLUE);
Getting an element by its ID is a fundamental skill in Android development. With this knowledge, you can manipulate the elements on the screen and create engaging user interfaces.