Android game development system control-Dialog

Source: Internet
Author: User

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

Related Article

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.