In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "how to use Android Studio View binding ViewBinding". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Android Studio's view binding ViewBinding.
View Binding is a feature that makes it easier for you to write code that interacts with the view. When view binding is enabled in a module, it generates a binding class for each XML layout file that exists in the module. An instance of the binding class contains a direct reference to all views that have ID in the corresponding layout.
Text
Modify build.gradle
Add the viewBinding element to its build.gradle file, which needs to be re-sync after the addition
/ / Android Studio 3.6.0android {... ViewBinding {enabled = true}}
In Android Studio 4. 0, viewBinding is integrated into the buildFeatures option, and the configuration is changed to:
/ / Android Studio 4.0android {buildFeatures {viewBinding = true}} modify java code
If your layout file is activity_main.xml, an ActivityMainBinding class will be generated, if your layout file is result_profile.xml, a ResultProfileBinding class will be generated, and so on.
Here is an example of activity_main.xml and its corresponding MainActivity.java:
Suppose activity_main.xml places three controls: TextView (Id is text), Button (Id is button), and ImageView (no Id is set), where ImageView cannot be referenced in the code because there is no reference to it in the binding class because Id is not set.
Part of the code for MainActivity.java is as follows:
@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); / / setContentView (R.layout.activity_main); LayoutInflater layoutInflater = LayoutInflater.from (this); ActivityMainBinding binding = ActivityMainBinding.inflate (layoutInflater); setContentView (binding.getRoot ()); binding.text.setText ("text has changed"); binding.button.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {Toast.makeText (getApplicationContext (), "Button clicked", Toast.LENGTH_SHORT). Show () });}
Matters needing attention
Note: the original setContentView (R.layout.activity_main) needs to be commented out, otherwise the ContentView will be set repeatedly.
The root view of the layout (activity_main.xml) automatically generates a member variable named rootView. In the onCreate () method of Activity, to pass rootView into the setContentView () method so that Activity can use the layout in the bound object, rootView is a private variable and needs to be obtained using the getRoot () method.
At this point, I believe you have a deeper understanding of "how to use Android Studio's view binding ViewBinding". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.