Analyze a simple dialog box in Android

Source: Internet
Author: User

A Simple Description Dialog BoxAndroidInstance, analyzes the principle, the following is the Code directly.

Step 1: first configure values/strings. xml in the res folder of the project

Java code:

 
 
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <Resources>
  3. <String name = "app_name"> normal dialog box </string>
  4. <String name = "button"> display normal dialog box </string>
  5. <String name = "title"> normal dialog box </string>
  6. <String name = "OK"> OK </string>
  7. <String name = "message"> This is a normal dialog box </string>
  8. </Resources>

Step 2: Configure layout/main. xml in the res folder of the project to create the input box and the "show normal dialog box" button.

Java code:

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3. android:orientation="vertical" 
  4. android:layout_width="fill_parent" 
  5. android:layout_height="wrap_content" 
  6. <EditText android:id="@+id/editText" 
  7. android:layout_width="fill_parent"   
  8. android:layout_height="wrap_content"   
  9. android:text="@string/message"/> 
  10. <Button android:id="@+id/button" 
  11. android:layout_width="fill_parent" 
  12. android:layout_height="wrap_content" 
  13. android:text="@string/button"/> 
  14. </LinearLayout> 

Step 3: Compile the. java file under the src folder

Java code:

 
 
  1. Package eoe. demo;
  2. Import android. app. Activity;
  3. Import android. app. AlertDialog;
  4. Import android. app. AlertDialog. Builder;
  5. Import android. app. Dialog;
  6. Import android. content. DialogInterface;
  7. Import android. OS. Bundle;
  8. Import android. util. Log;
  9. Import android. view. View;
  10. Import android. widget. Button;
  11. Import android. widget. EditText;
  12. Public class CommonDialogActivity extends Activity {
  13. Protected static final int COMMON_DIALOG = 1;
  14. /** Called when the activity is first created .*/
  15. @ Override
  16. Public void onCreate (Bundle savedInstanceState ){
  17. Super. onCreate (savedInstanceState );
  18. SetContentView (R. layout. main );
  19. Button button = (Button) findViewById (R. id. button );
  20. View. OnClickListener listener = new View. OnClickListener (){
  21. Public void onClick (View arg0 ){
  22. // TODO Auto-generated method stub
  23. ShowDialog (COMMON_DIALOG );
  24. }
  25. };
  26. Button. setOnClickListener (listener );
  27. }
  28. /*
  29. * Use onCreateDialog (int) to manage the status of the dialog box,
  30. * When the dialog box is released,
  31. * The status of the dialog box object will be saved by Activity. Call
  32. * RemoveDialog (int) references all the internal references of this object.
  33. * If removeDialog is not added as in this program,
  34. * The first content is displayed.
  35. */
  36. Protected Dialog onCreateDialog (int id ){
  37. AlertDialog dialog = null;
  38. EditText editText = (EditText) findViewById (R. id. editText );
  39. Switch (id ){
  40. Case COMMON_DIALOG:
  41. Builder builder = new AlertDialog. Builder (this );
  42. Builder. setIcon (R. drawable. icon );
  43. Builder. setMessage (editText. getText ());
  44. Builder. setTitle (R. string. title );
  45. DialogInterface. OnClickListener listener = new DialogInterface. OnClickListener (){
  46. Public void onClick (DialogInterface arg0, int arg1 ){
  47. // TODO Auto-generated method stub
  48. RemoveDialog (COMMON_DIALOG );
  49. }
  50. };
  51. Builder. setPositiveButton (R. string. OK, listener );
  52. Dialog = builder. create ();
  53. Break;
  54. Default:
  55. Break;
  56. }
  57. Log. e ("onCreateDialog", "onCreateDialog ");
  58. Return dialog;
  59. }
  60. }

Android layout attributes

Ten practical tips for the Android 2.2 System

Google Android UI design skills: Excellent UI design principles

Google Android UI design tips: icons and instructions

The Android practice system dialog box enables login Registration

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.