Note: The notes written earlier are put together.
Article 3 intermediate (1)
Chapter 12
This chapter focuses on the MVC mode. In practice, we have adopted the following practices:
1. Extract all the code of "declaration and search interface component" (findviewbyid) to findviews;
2. Extract all the code "add control flow for specific interface components" to setlisteners;
3. Call findviews and setlisteners in the oncreate method to implement declaration and separation of Program Logic and interface components.
Chapter 13 add dialog box (DIALOG)
1. Example code of a dialog box:
Private void openoptionsdialog (){
New alertdialog. Builder (BMI. This ).
. Settitle (R. String. about_title)
. Setmessage (R. String. about_time)
. Setpositionbutton ("OK ",
New dialoginterface. onclicklistener (){
Public void onclick (
Dialoginterface, int I ){
}
})
. Show ();
Note:
A uses the anonymous entity method here. It is said that the advantage of doing so is: "After this dialog box is run, the system automatically recycles the memory space occupied by this anonymous entity. ";
B BMI. This is the reference of the instance of the activity class in which the dialog box is located;
C. The dialog box contains a "OK" button.
2. Example of adding a listener object:
Add Android: onclick = "openoptionsdialog" to the XML file of the button, and define the following public void openoptionsdialog (view target) function ){}
3. display the toast code example
Toast. maketext (BMI. This, "BMI calculator", Toast. length_short). Show ();
4. handle errors
Use the try... catch... exception handling mechanism.
Chapter 14 view online content (URI)
1. Add a button to the dialog box. Insert the following method after the setpositionbutton method:
. Setnegativebutton (R. String. homepage_label,
New dialoginterface. onclicklistener (){
Public void onclick (
Dialoginterface, int I ){
......
}
})
This should have been added to the cancel button (negative and positive). The author uses it as a button to access the Home Page Link.
2. example of how to open a connection, phone number, or map
Uri uri = URI. parse ("http://sites.google.com/site/gasodroid ");
Intent intent = new intent (intent. action_view, Uri );
Startactivity (intent );
Open the webpage link above (start the default browser );
If you change the parse parameter to "Geo: 25.047192, 121.516981", Google map is started to locate the specified coordinate;
Change the parse parameter to "Tel: 12345678" and the action_view parameter to action_dial, and then dial.
Personally, the code above can also be rewritten using an anonymous entity, for example:
Startactivity (new intent (intent. action_view, Uri. parse (target ));
Chapter 15 Add menu)
Add the menu to implement two functions: oncreateoptionsmenu and onoptionsitemselected. For example:
Protected static final int menu_about = menu. first;
Protected static final int menu_quit = menu. First + 1;
@ Override
Public Boolean oncreateoptionsmenu (menu ){
Menu. Add (0, menu_about, 0, "about ...")
. Seticon (Android. R. drawable. ic_menu_help );
Menu. Add (0, menu_quit, 0, "end ")
. Seticon (Android. R. drawable. ic_menu_close_clear_cancel );
Return super. oncreateoptionsmenu (menu );
}
Public Boolean onoptionsitemselected (menuitem item ){
Switch (item. getitemid ()){
Case menu_about:
Openoptionsdialog ();
Break;
Case menu_quit:
Finish ();
Break;
}
Return super. onoptionsitemselected (item );
}
The previous method is to create a menu, and the latter is to process the option action. In addition, the resource reference method of andriod. R. XXX. XXX is used to reference the built-in resources of the system. The difference between this method and R. XXX. XXX is that there is an additional "android.".
Chapter 16 defines the android list (manifest)
In fact, this chapter is not very clear about the manifest configuration file. From a macro perspective, this book is suitable for the first book to learn Android, but not the only one.