Disclaimer: The book "Secrets of Android Application Development", which records the logs of the book, references the relevant code and summary, and has no commercial use, it is completely a record of self-learning, and many problems will inevitably occur in learning just now. If there are any mistakes, please criticize them a lot.
1. Button)
The button control has been used in previous examples. It is mainly a setonclicklistener event.
Ii. Menu)
Generally, Android phones use the menu button to display the corresponding menu. To achieve the menu effect, you need to use oncreateoptionsmenu and then listen on its events to implement different tasks, you can achieve this in two ways:
1. Implemented through XML layout;
2. Implemented through menu. Add
Example Analysis: The Menu effect is achieved through the above two methods. Main. XML is implemented using the XML layout method, and main2.xml is implemented using the menu. Add method.
1. xml layout:
Create a menu folder in the res directory, and create a menu. xml file in the directory to create the required menu.
<Menu xmlns: Android = "http://schemas.android.com/apk/res/android"> <item Android: Id = "@ + ID/about" Android: Title = "about"/> <item Android: id = "@ + ID/exit" Android: Title = "quit"/> </menu>
Then, activity01 overwrites the oncreateoptionsmenu method to load the menu file.
// Create menupublic Boolean oncreateoptionsmenu (menu) {menuinflater Inflater = This. getmenuinflater (); // set the menu interface to RES/menu. xmlinflater. inflate (R. menu. menu, menu); Return true ;}
Finally, you can add a listener for menu button events.
// Process the menu event public Boolean onoptionsitemselected (menuitem item) {// todo auto-generated method stub // obtain the currently selected idint item_id = item. getitemid (); Switch (item_id) {case R. id. about: // create an intent object. For intent usage, see intent = new intent (); // specify the intent class to be started. setclass (examples_04_13activity.this, examples_04_13activity02.class); // start a new activitystartactivity (intent); // close the current activity (); break; case R. id. exit: examples_04_13activity.this.finish (); break;} return true ;}
[Extension point]: understanding and using intent
Intent is an abstract description of the action to be executed. It is generally used as a parameter and is used by intent to facilitate communication between Android components. For example, startactivity () is called to start an activity, or broadcaseintent () is used to transmit it to all interested broadcasereceiver, or startservice ()/bindservice () to start a backend service. as you can see, intent is mainly used to start other activities or services, so it can be understood as the adhesive between activities.
See the article http://wenku.baidu.com/view/76db47858762caaedd33d452.html
2. Use the menu. Add method:
You can use the menu. Add method to add the oncreateoptionsmenu method, as shown in;
Public Boolean oncreateoptionsmenu (menu) {// Add content menu for menu. add (0, 0, 0, R. string. OK); menu. add (0, 0, 1, R. string. back); Return true ;}
Instance effect: the first implementation method is displayed on the left, and the second implementation method is displayed on the right.
3. Dialog Box (DIALOG)
To implement the dialog box effect, you can use the alertdialog. Builder class and customize the dialog box.
Key instance source code analysis:
Dialog dialog = new alertdialog. Builder (examples_04_14activity.this). settitle ("Logon prompt") // set the title. setmessage ("You need to log in here! ") // Set the content. setpositivebutton ("OK", // set the OK button new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int which) {// todo auto-generated method stub // click "OK" to go To the login box. layoutinflater factory = layoutinflater. from (examples_04_14activity.this); // obtain the custom dialog box final view dialogview = factory. inflate (R. layout. dialog, null); alertdialog DLG = new alertdialog. builder (examples_04_14activity.this ). settitle ("login box "). setview (dialogview) // sets the style of the custom dialog box. setpositivebutton ("OK", // set "OK" button new dialoginterface. onclicklistener () {// set the event listener public void onclick (dialoginterface dialog, int whichbutton) {// After the input is complete, click "OK" to log on to m_dialog = progressdialog. show (examples_04_14activity.this, "Please wait... "," logging in for you... ", true); New thread () {public void run () {try {sleep (3000);} catch (exception e) {e. printstacktrace ();} finally {// login is complete. Cancel m_dialog dialog box m_dialog.dismiss ();}}}. start ();}}). setnegativebutton ("cancel", // set the "cancel" button new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int whichbutton) {// click the "cancel" button and exit the program examples_04_14activity.this.finish ();}}). create (); // create a DLG. show (); // display }}). setneutralbutton ("exit", new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int whichbutton) {// click the "exit" button to launch the program examples_04_14activity.this.finish ();}}). create (); // create button // display dialog box dialog. show ();
[Expansion point]
1. layoutinflater factory = layoutinflater.From(Activity01.This);
Inflater indicates expansion. Layoutinflater is similar to findviewbyid (). The difference is that layoutinflater is used to find and instantiate an XML layout file in the layout folder! Findviewbyid () is used to find the widget controls (such as buttons and textview) in a specific XML file ).
Summary:
(1). In general, we use layoutinflater to do one thing: inflate. The inflate method has four forms in total to convert layout expressed in XML into view.
(2) three methods for obtaining layoutinflater:
Layoutinflater Inflater = (layoutinflater) context. getsystemservice (context. layout_inflater_service); view layout = Inflater. inflate (R. layout. main, null); layoutinflater Inflater = layoutinflater. from (context); (this method is actually the first method, refer to the source code) view layout = Inflater. inflate (R. layout. main, null); layoutinflater Inflater = getlayoutinflater (); (this function can be used in the activity. It is actually a function of window under the view subclass.) view layout = Inflater. inflate (R. layout. main, null );
(3) differences between setcontentview and inflate:
public class MyInflate extends Activity{private TextView tv;public void OnCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);//setContentView(R.layout.main);//tv = (TextView) findViewById(R.id.tv);LayoutInflater inflate = LayoutInflater.from(this);View view = inflate.inflate(R.layout.main,null);setContentView(view);}}
The Code commented out above is the same as the Code not commented out.
Differences:
Once setcontentview () is called, layout will immediately display the UI. Inflate will only form a ready-made object in the View class and display it with setcontentview (View) when necessary. Generally, the interface is displayed through setcontentview () in the activity, but if you want to set the layout of the control in a non-activity, layoutinflater needs to be dynamically loaded.
Public View inflate (INT resourece, viewgroup root)
Purpose: Fill in a new view hierarchy from the specified XML resource file.
Resource: The layout ID of the view.
Root: Root View of the generated hierarchy
Return fill the Root View of the hierarchy. If the root parameter is provided, root is the root view; otherwise, the root of the filled XML file is the root view.
The other several overloaded inflate functions are similar.
The difference is referenced from: http://www.cnblogs.com/sanjinxiong/articles/2125142.html
2.Use of alertdialog. Builder ()
Alertdialog is a direct subclass of dialog, and alertdialog is also one of the most commonly used dialogs in the Android system.
An alertdialog can have more than two buttons, and you can set the corresponding information for an alertdialog. For example, title, massage, setsinglechoiceitems, setpositivebutton, setnegativebutton, and so on ....
However, you cannot directly use the constructor of alertdialog to generate an alertdialog. Studying the source code of alertdialog, it is found that all the constructor methods of alertdialog are write-protected, so they cannot be obtained through: alertdialog = new alertdialog. You can only use:
Alertdialog. Builder alertdialog = new alertdialog. Builder (this );
Iv. Image view)
For the imageview object, you can use setimageresource () to set the resource index for the image to be displayed.
Instance analysis: implements the decrease of the Alpha value of the imageview object.
Key source code:
Public class activity01 extends activity {imageviewimageview; // declare the imageview object textviewtextview; intimage_alpha = 255; // The Alpha value of imageview, handlermhandler = new handler (); booleanisrung = false; // control thread public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); isrung = true; // obtain the imageview object imageview = (imageview) This. findviewbyid (R. id. imageview01); textview = (Textview) This. findviewbyid (R. Id. textview01); // sets the image resource of imageview. Similarly, you can write/Android: src = "@ drawable/logo" imageview as follows in the XML layout. setimageresource (R. drawable. logo); // set the Alpha value of imageview to imageview. setalpha (image_alpha); // start a thread to reduce the Alpha value by new thread (New runnable () {public void run () {While (isrung) {try {thread. sleep (200); // update the Alpha value updatealpha ();} catch (interruptedexception e) {e. printstacktrace ();}}}}). start (); // update the imageview view mhandler = new handler () {@ overridepublic void handlemessage (Message MSG) {super. handlemessage (MSG); imageview. setalpha (image_alpha); textview. settext ("the current alpha value is:" + integer. tostring (image_alpha); // update the imageview. invalidate () ;}};} public void updatealpha () {If (image_alpha-7> = 0) {image_alpha-= 7 ;}else {image_alpha = 0; isrung = false;} // send the message mhandler that needs to update the imageview. sendmessage (mhandler. obtainmessage ());}}
[Extension point] use of handler
The handler class allows you to send messages and process messages and runnable objects in the thread message queue. The handler instance is used together with a thread and the message queue of the thread. Once a new handler instance is created, the system binds the instance to a message queue of the thread and the thread, which can send messages and runnable objects to the message queue and process them at the Message Queue exit.
Handler class has two main purposes: 1. According to the schedule, a runnable instance is processed or executed at a certain time point in the future. 2. Put an operation request to another thread object in the message queue to avoid inter-thread conflicts.
Time-based messages are used as follows: Post (runnable), postattime (runnable, long), postdelayed (runnable, long), sendemptymessage (Int), Sendmessage (Message), sendmessageattime (message, long), and sendmessagedelayed (message, long)
Functions such as methods. Post can transmit a runnable object to the message queue and be called after it reaches the message queue.SendmessageFunctions like this can transmit a message object containing data, which can be processed by the handlemessage (Message) method of the handler class.
Functions such as post andSendmessageSuch functions can specify the message execution time, namely immediate execution, execution later, or execution at a certain time. This can be used for timeout, message, or other time-related operations.
When a process starts, the main thread executes a message queue independently, which manages objects (such as activities and broadcast receivers) at the top layer of the application and all created windows. You can create your own thread and communicate with the main thread through handler. This can be done by calling the post andSendmessageOperation.
1. imagebutton)
The imagebutton object uses the setimagedrawable () method to set the display image.
The basic control is almost learned here. Below are some effects.
Go to P92