Details that should be paid attention to in Android

Source: Internet
Author: User

Original post address: http://wayfarer.javaeye.com/blog/444061

(1) the names of XML files in res must be all lowercase letters. If there are uppercase letters, compilation will fail. The reason is unknown.


(2) statement order in the oncreate () method

Java code
  1. <Span style = "font-size: Small">Protected VoidOncreate (bundle savedinstancestate ){
  2. Super. Oncreate (savedinstancestate); // The Order is 1. The reason is simple because it is a super statement.
  3. Setcontentview (R. layout. checkbox); // The Order is 2. Unlike j2's, this statement must be placed before other widgets and the widgets placed before it will not be loaded into this activity.
  4. TV = (textview) findviewbyid (R. Id. textview );
  5. Apple = (checkbox) findviewbyid (R. Id. Apple );
  6. ......
  7. } </Span>
Protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // The Order is 1. The reason is simple, because it is a super statement setcontentview (R. layout. checkbox); // The Order is 2. Different from j2's, this statement must be placed before other widgets, And the widgets placed before it will not be loaded into this activity. TV = (textview) findviewbyid (R. id. textview); Apple = (checkbox) findviewbyid (R. id. apple );......}

Requestwindowfeature (window. feature_no_title); makes the activity have no titlebar, and the display area becomes larger. If this statement is required, it should be placed before the setcontentview () method.


(3) XML attributes

1) spinner/gridview/listview


2) @ + id
@ + Id indicates the ID of the new control added by the individual. "+" indicates the new property; @ ID indicates the android built-in property; @ Android: ID/empty indicates the free property in Android.


3) layout_width and layout_height are essential attributes: Java. Lang. runtimeexception: You must supply a layout_width attribut.

4) Java. Lang. illegalstateexception: scrollview can host only one direct child.


(4) control the display and hiding of the keyboard

Dispatchkeyevent (New keyevent (keyevent. action_down, keyevent. keycode_back ));
Dispatchkeyevent (New keyevent (keyevent. action_up, keyevent. keycode_back ));


(5) What should I do if your background activity is recycled by the system: onsaveinstancestate?
When one activity a in your program is running, it actively or passively runs another activity B. At this time, a will execute onsaveinstancestate ()
B will come to a again after it is completed. There are two situations at this time: A is recycled, and A is not recycled. A will call oncreate () again when it is recycled () the method is different from the method in which the savedinstancestate parameter is included in oncreate (). If it is not recovered, the onresume () is executed directly and the oncreate () is skipped.
Savedinstancestate is a bundle object. You can basically regard it as a map object maintained by the system. You may use it in oncreate (). If oncreate () is started normally, it will not be available. Therefore, you need to determine whether it is empty when using it.
Just like in the official notepad tutorial, if you are editing a note that is suddenly interrupted, remember the Note ID, then, you can extract the note according to the ID, and the program will be complete. This also shows that your application does not need to be saved. For example, if your interface is to read a list, you do not need to remember anything special. Oh, maybe you need to remember the position of the scroll bar...


(6) alertdialog

The alertdialog class is a subclass of the dialog class. It provides three buttons and a text message by default. These buttons can be displayed or hidden as needed.
The alertdialog class has an internal class named 'builder'. The Builder class provides the ability to add multiple-choice or single-choice lists for the dialog box and add event processing for these lists. In addition, the builder class calls the three buttons in the alertdialog dialog box positivebutton, neutralbutton, and negativebutton according to their respective positions.

  • The size is 24.2 kb.
  • The size is 30.8 kb.
  • The size is 166.6 kb.

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.