Android game development system control-Dialog
Dialog (Dialog box) is often used in Android Application Development. The following is a book on "Android game programming starts from scratch" and a preliminary study on Dialog.
Create a project: DialogProject
Function: displays a dialog box with TextView and buttons.
Simple dialog box:
Dialog box for adding a ticket:
Dialog box for adding multiple selection boxes:
Add List dialog box:
Dialog box for adding a custom Layout:
Project source code:
>>> Main. xml
[Html]
• <? Xml version = "1.0" encoding = "UTF-8"?>
• <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
• Android: layout_width = "fill_parent"
• Android: layout_height = "fill_parent"
• Android: orientation = "vertical">
•
• <TextView
• Android: layout_width = "fill_parent"
• Android: layout_height = "wrap_content"
• Android: text = "@ string/hello"/>
•
• </LinearLayout>
>>> Dialogmain. xml
[Html]
• <? Xml version = "1.0" encoding = "UTF-8"?>
• <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
• Android: layout_height = "wrap_content"
• Android: layout_width = "wrap_content"
• Android: background = "# ffffffff"
• Android: orientation = "horizontal"
• Android: id = "@ + id/myLayout"
•>
• <TextView
• Android: layout_width = "wrap_content"
• Android: layout_height = "wrap_content"
• Android: text = "TextView"/>
• <EditText
• Android: layout_width = "wrap_content"
• Android: layout_height = "wrap_content"
•/>
• <Button
• Android: layout_width = "wrap_content"
• Android: layout_height = "wrap_content"
• Android: text = "btn1"
•/>
• <Button
• Android: layout_width = "wrap_content"
• Android: layout_height = "wrap_content"
• Android: text = "btn2"
•/>
• </LinearLayout>
>>> DialogProjectActivity. java
[Java]
• Package com. dialog;
•
• Import android. app. Activity;
• Import android. app. AlertDialog. Builder;
• Import android. content. DialogInterface;
• Import android. content. DialogInterface. OnClickListener;
• Import android. OS. Bundle;
• Import android. view. LayoutInflater;
• Import android. view. View;
• Import android. view. ViewGroup;
•
• Public class DialogProjectActivity extends Activity {
• Private Builder builder; // declare the Bulider object
•/** Called when the activity is first created .*/
• @ Override
• Public void onCreate (Bundle savedInstanceState ){
• Super. onCreate (savedInstanceState );
• SetContentView (R. layout. main );
• // Instantiate the Builder object
• Builder = new Builder (DialogProjectActivity. this );
• // Set the icon in the dialog box
• Builder. setIcon (android. R. drawable. ic_dialog_info );
• // Set the title of the dialog box
• Builder. setTitle ("Dialog ");
• // Prompt text in the Setting Dialog Box
• // Builder. setMessage ("Dialog box ");
• // Listener button on the left
• Builder. setPositiveButton ("Yes", new OnClickListener (){
• Public void onClick (DialogInterface dialog, int which ){
•
•}
•});
• // Button on the right of the listener
• Builder. setNegativeButton ("No", new OnClickListener (){
• Public void onClick (DialogInterface dialog, int which ){
•
•}
•});
•/*
• // Add radio button
• Builder. setSingleChoiceItems (new String [] {"single choice", "single choice"}, 1, new
• OnClickListener (){
• Public void onClick (DialogInterface dialog, int which ){
• // Which: select the subscript
•}
•});
•
• // Add check boxes
• Builder. setMultiChoiceItems (new String [] {"multiple choice", "multiple choice "},
• New boolean [] {false, true}, new OnMultiChoiceClickListener (){
• Public void onClick (DialogInterface dialog, int which, boolean isChecked ){
• // Which: select the subscript
• // IsChecked: Check status of the selected item
•}
•});
•
• // Add a list item
• Builder. setItems (new String [] {"list item 1", "list item 2", "list item 3 "},
• New OnClickListener (){
• Public void onClick (DialogInterface dialog, int which ){
• // Which: select the subscript
•}
•});
•*/
• // Instance layout
• LayoutInflater inflater = getLayoutInflater ();
• View layout = inflater. inflate (R. layout. dialogmain, (ViewGroup) findViewById (R. id. myLayout ));
• Builder. setView (layout );
• // Call the show () method to display the dialog box
• Builder. show ();
•}
•}
Author: com_xp