Since the graphical application is available, the Dialog box (Dialog) has been a component (widget ). The Components Library of Android considers small touch screens. In terms of the design of basic components, Android has also made considerate designs for users. For Android mobile apps, components that are frequently used cannot be as complex as click-based systems. In terms of usability, the commonly used components are as follows:
Menu)
Dialog Box (Dialog)
Fast Display (Toast)
Using the above three components and their "change body", you can form a user-friendly application interface. In addition, Android features for mobile phone operations, the component library is well designed for use. Therefore, a comfortable and friendly operation interface can be provided when few components are used.
The concept of the dialog box is not introduced much. After learning windows graphics programming, it is basically getting started from the dialog box. It is estimated that many people will feel like they saw the dialog box like me. The Dialog box object provided by Android is android. app. Dialog. inheriting the Dialog AlertDialog is the most basic Dialog box. AlertDialog can be used to increase the user interaction experience of the application and increase the interface friendliness of the program. The following describes the design method of AlertDialog:
Continue the WebTest example and add the following scenarios:
Press the Menu key.
Select the "Play" option
A dialog box is displayed, asking whether the user is sure "Yes/No"
From the above usage scenarios, when judging the R. id. play project in onOptionsItemSelected (), create a dialog box on the UI. The following code is used:
[Java]
@ Override
Public boolean onOptionsItemSelected (MenuItem item ){
// TODO Auto-generated method stub
Int item_id = item. getItemId ();
Switch (item_id ){
Case R. id. play:
AlertDialog. Builder builder = new AlertDialog. Builder (this );
Builder. setMessage ("Are you sure? ");
Builder. setCancelable (false );
Builder. setPositiveButton ("Yes", new DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog, int id ){
Intent intent = new Intent (WebTestActivity. this, yypService. class );
StartService (intent );
Toast. makeText (WebTestActivity. this, "It's playing music.", Toast. LENGTH_LONG). show ();
}
});
Builder. setNegativeButton ("No", new DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog, int id ){
}
});
AlertDialog alert = builder. create ();
Alert. show ();
Break;
Case R. id. stop:
This. onStop ();
Toast. makeText (this, "music is stopped.", Toast. LENGTH_LONG). show ();
Break;
Default:
Return false;
}
Return true;
}
AlertDialog description:
Create the AlertDialog. builder object (dialog builder), which is an object used to create the content of the dialog box;
Set the display information of dialog builder -- builder. setMessage ();
Set whether the dialog box can be canceled-builder. setCancelable ();
Use dialog builder to generate two buttons (Yes/No) in the dialog box -- builder. setPositiveButton () and builder. setNegativeButton ()
Use dialog builder to create an AlertDialog object. AlertDialog is a real dialog box object-builder. create ()
Display AlertDialog on the UI-alert. show ()
The program that generates the "Yes button" is:
[Java]
Builder. setPositiveButton ("Yes", new DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog, int id ){
}
});
Call the builder. setPositiveButton () method to create a "Yes" button. The parameter description is as follows:
1. First parameter: Text displayed on the button
2. Second parameter: Specify the click listener
Each button requires a click listener. When you press the button, the click listener is called back.
The program execution results are as follows:
From Young's column