Understanding Android callback Functions

Source: Internet
Author: User

I think TextView is one of the first controls we have come into contact with Android. Therefore, from the beginning of TextView, we can all understand that word understanding is used for "text viewing, yes, the actual application also plays this role. Most of the books are called text boxes. EditText and Button continue with TextView.

As for callback functions, we can't do without them as long as we start to play Android. This is because the Android program uses the framework structure, which is a convenient and simple feature of Android. This is also a double-edged arrow, for beginners, this is also hard to understand, because we sometimes feel like filling in the blank question to knock the code into the method provided by Android, I don't know how the Android app framework layer can help us with some operations. Although the app framework layer provides APIs to associate with the original code, press Ctrl and click the functions provided by Android with the mouse, ...), However, he is not fully open to third parties. Some of them can't be understood at first, because they are not written by us. They have different ways of thinking and are hard to understand. Unlike C and C ++, it is easy to understand. Okay, I will not talk about it anymore. Let's take a look at two examples.

The purpose of the two examples: setContentView, how to call the XML file, and how to write non-automatically generated code. The execution results of these two examples are the same.

Example 1:

Protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); TextView textView = new TextView (this); // create an object and point it to its own textView. setText ("Test"); // set the display content for the object setContentView (textView); // load the object content and deliver it to the UI}

Example 2:

Protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // load the XML file setContentView (R. layout. activity_main); // find the object using idTextView textView = (TextView) findViewById (R. id. textView1); // sets the textView of the object display content. setText ("Test ");}

Example 2: The system calls the setContentView method to load the layout list file. layout. activity_main), use the findViewById method in the list file to find the ID of the display component we selected here is TextView), this ID is equivalent to our ID card, unique, can not be repeated. Find the ID and assign it to the TextView object reference. Then, set the content to be displayed and send it to the UI ).

To sum up, Example 1 shows that the content to be displayed by the setContentView method is directly transmitted to the UI interface. No other methods are called. In Example 2, The setContentView method loads the activity_main file and calls the findViewById method to find the ID.

Speaking of this, the callback function will also be mentioned later when we talk about the result code and thread SyncTask.

Next article: ListeView principle and optimization I)

This article is from the "Android key" blog, please be sure to keep this source http://7585106.blog.51cto.com/7575106/1298889

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.