Android experiment: What will happen if the view ID is the same?

Source: Internet
Author: User

1. Experiment: Use the layout editor to forcibly specify the two buttons with the same ID, and then Code Obtain the handle through findviewbyid () and modify its text.  
Experimental results: only the text of one button changes, and the other is not affected.  
2. Experiment: Only one linearlayout is put in the main layout, and a new button is dynamically added in the code, which is added multiple times with layout. addview.  
Experiment results: discovery Program An error is reported directly to prevent running.  
3. Experiment: Put two linearlayout in the main layout. After a new button is added to the Code, add it to two different layout. addview () Layout respectively.  
Experiment results: The program directly reports an error and does not run.  
4. experiment: put a button ID btn001 in the active activity layout. Create a dialog box and place a button in the layout. The ID is btn001, in the activity and dialog oncreate functions, you can use findviewbyid to search for BTN and modify the text.  
Experiment results: the text of both buttons is changed to their own text, which does not affect each other.  
5. Experiment: Create a button through new in activity, add the new button to activity, create a dialog box, and add the button to the dialog box.  
Experiment result: An error occurred while running the program.  


conclusion:
1. the view ID in the same project can be the same. In the r file, the same ID only generates one data item.
2. When two views with the same ID are displayed in the same view, only one of them can be accessed through findviewbyid.
3. the same view (the new handle) can only be managed by one view, and cannot be added to another view or multiple views multiple times. Otherwise, the program runs incorrectly.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.