[Reading Notes-Android game programming starts from scratch] 7. Commonly Used system controls for Android game development (Dialog)

Source: Internet
Author: User

In Android Application Development, Dialog (Dialog box) is often used for simple creation and easy management. The default style of the Dialog box is similar to the Activity of creating a style.
First, we will introduce the Builder class under android. app. AlertDialog. Builder is a subclass of the AlertDialog class and its internal class. As shown in its name, Builder is equivalent to a specific constructor. It sets the dialog box attributes through Builder and then displays the Builder (dialog box.

I made a Demo of the Dialog display effect set. The effect is as follows (the GIF image is large and requires loading time ):

 

Main Code:

Package yc. example. dialogshow; import android. app. activity; import android. app. alertDialog; import android. app. dialog; import android. app. progressDialog; 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; import android. widget. button; import android. Widget. editText; import android. widget. imageView; import android. widget. toast; public class MainActivity extends Activity {private static AlertDialog. builder builder; private static Dialog dialog; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} public void Btn1_Onclick (View view) {// instantiate Builder builder = n Ew AlertDialog. builder (MainActivity. this); // set the icon in the dialog box // builder. setIcon (R. drawable. ic_launcher); builder. setIcon (android. r. drawable. ic_dialog_info); // set the icon builder in the dialog box. setTitle ("dialog box title"); // set the prompt text builder in the dialog box. setMessage ("prompt text in the dialog box"); builder. setPositiveButton ("OK", new OnClickListener () {@ Override public void onClick (DialogInterface arg0, int arg1) {Toast. makeText (MainActivity. this, "this is a simple conversation. Box ", Toast. LENGTH_SHORT ). show () ;}}); // call the show () method to display the dialog box builder. show ();} public void Btn2_Onclick (View view) {dialog = new AlertDialog. builder (this ). setIcon (R. drawable. ic_launcher ). setTitle ("prompt "). setMessage ("are you sure you want to exit? "). SetPositiveButton ("OK", null ). setNegativeButton ("cancel", null ). show ();} public void Btn3_Onclick (View view) {dialog = new AlertDialog. builder (this ). setIcon (android. r. drawable. btn_star ). setTitle ("Preferences "). setMessage ("do you like LOL? "). SetPositiveButton ("very fond", null) // listen to the P-type button, positive ;. setNegativeButton ("dislike", null) // listen to N-type buttons, negative ;. setNeutralButton ("General", null) // listener neutral button, usually in the middle. show ();} public void Btn4_Onclick (View view) {dialog = new AlertDialog. builder (this ). setIcon (android. r. drawable. ic_dialog_info ). the setTitle ("enter")/** setView () method is to add system components to the dialog box, but only one component can be set. If you use this method multiple times, the first set component will be replaced by the later set component * setView () to add a system component layout, which is placed at the bottom of the dialog box by default (button above )*/. set View (new EditText (this )). setPositiveButton ("OK", null ). setNegativeButton ("cancel", null ). show ();} public void Btn5_Onclick (View view) {final String [] WuJiang = new String [] {"Guan Yu", "Zhang Fei", "Lu Bu", "Zhao Yun ", "Ma Chao"}; dialog = new AlertDialog. builder (this ). setTitle ("Please select your initial military commanders "). setIcon (android. r. drawable. ic_dialog_info)/** Method for adding a single sequence: setSingleChoiceItems (CharSequence [] items, int * checkedItem, OnClickListe Ner listener) items: indicates the texts of the single choice * checkedItem: indicates the default subscript listener selected in the single choice: Click the listener */. setSingleChoiceItems (WuJiang, 0, new OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {Toast. makeText (MainActivity. this, "the secret you selected is:" + WuJiang [which], Toast. LENGTH_SHORT ). show (); dialog. dismiss ();}}). show ();} public void Btn6_Onclick (View view) {dialog = new AlertDialog. builder (this ). SetTitle ("select your favorite sports")/** how to add check boxes: setMultiChoiceItems (CharSequence [] items, boolean [] * checkedItems, OnMultiChoiceClickListener listener) * items: indicates the text of the selected text; checkedItems: indicates the selected status of the selected text; listener: Select multiple click listeners */. setMultiChoiceItems (new String [] {"basketball", "soccer", "swimming", "Racing", "running"}, null, null ). setPositiveButton ("OK", null ). setNegativeButton ("cancel", null ). show ();} public void Btn7_Onclick (View view) {Dialog = new AlertDialog. builder (this ). setTitle (" "). setItems (new String [] {"Lu Fengxian", "Zhao Zilong", "Lai dianwei", "Guan Yunchang", "Zhang yide"}, null ). setPositiveButton ("OK", null ). setNegativeButton ("cancel", null ). show ();} public void Btn8_Onclick (View view) {ImageView img = new ImageView (this); img. setImageResource (R. drawable. goodby_times); dialog = new AlertDialog. builder (this ). setTitle ("Goodbye, time! "). SetView (img ). setPositiveButton ("OK", null ). show ();} public void Btn9_Onclick (View view) {// instantiate Layout layout LayoutInflater inflater = getLayoutInflater (); View Layout = inflater. inflate (R. layout. dialog1, (ViewGroup) findViewById (R. id. dialog_layout); Button btn_Login = (Button) layout. findViewById (R. id. btn_Login); final EditText uname = (EditText) layout. findViewById (R. id. uname); final EditText pwd = (EditText) layout. findViewById (R. id. pwd); btn_Login.setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {Toast. makeText (MainActivity. this, "username:" + uname. getText () + "\ n password:" + pwd. getText (), Toast. LENGTH_SHORT ). show (); dialog. dismiss () ;}}); dialog = new AlertDialog. builder (this ). setTitle ("User Logon "). setView (layout ). show () ;}// the progress reading box needs to simulate reading private ProgressDialog ss= null; private final static int MAX_READPROCESS = 100; public void Btn10_Onclick (View view View) {progressDialog = new ProgressDialog (MainActivity. this); progressDialog. setIcon (android. r. drawable. ic_dialog_info); progressDialog. setProgress (0); progressDialog. setTitle ("downloading the program... "); progressDialog. setProgressStyle (ProgressDialog. STYLE_HORIZONTAL); progressDialog. setMax (MAX_READPROCESS); progressDialog. show (); new Thread (progress ). start () ;}// start a new thread, accumulate the loop until 100, and then stop Runnable progress = new Runnable () {@ Override public void run () {int Progress = 0; while (Progress <MAX_READPROCESS) {try {Thread. sleep (50); progressDialog. setProgress (++ Progress);} catch (InterruptedException e) {e. printStackTrace () ;}/// after reading, the window automatically disappears progressDialog. cancel () ;}}; public void Btn11_Onclick (View view) {progressDialog = new ProgressDialog (MainActivity. this); progressDialog. setMessage ("loading data for you... "); progressDialog. setIndeterminate (true); progressDialog. setCancelable (true); progressDialog. show ();}}MainActivity. class

 

Source code: DialogShow.zip

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.