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:
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <Resources>
- <String name = "app_name"> normal dialog box </string>
- <String name = "button"> display normal dialog box </string>
- <String name = "title"> normal dialog box </string>
- <String name = "OK"> OK </string>
- <String name = "message"> This is a normal dialog box </string>
- </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:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- <EditText android:id="@+id/editText"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/message"/>
- <Button android:id="@+id/button"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/button"/>
- </LinearLayout>
Step 3: Compile the. java file under the src folder
Java code:
- Package eoe. demo;
- Import android. app. Activity;
- Import android. app. AlertDialog;
- Import android. app. AlertDialog. Builder;
- Import android. app. Dialog;
- Import android. content. DialogInterface;
- Import android. OS. Bundle;
- Import android. util. Log;
- Import android. view. View;
- Import android. widget. Button;
- Import android. widget. EditText;
- Public class CommonDialogActivity extends Activity {
- Protected static final int COMMON_DIALOG = 1;
- /** Called when the activity is first created .*/
- @ Override
- Public void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. main );
- Button button = (Button) findViewById (R. id. button );
- View. OnClickListener listener = new View. OnClickListener (){
- Public void onClick (View arg0 ){
- // TODO Auto-generated method stub
- ShowDialog (COMMON_DIALOG );
- }
- };
- Button. setOnClickListener (listener );
- }
- /*
- * Use onCreateDialog (int) to manage the status of the dialog box,
- * When the dialog box is released,
- * The status of the dialog box object will be saved by Activity. Call
- * RemoveDialog (int) references all the internal references of this object.
- * If removeDialog is not added as in this program,
- * The first content is displayed.
- */
- Protected Dialog onCreateDialog (int id ){
- AlertDialog dialog = null;
- EditText editText = (EditText) findViewById (R. id. editText );
- Switch (id ){
- Case COMMON_DIALOG:
- Builder builder = new AlertDialog. Builder (this );
- Builder. setIcon (R. drawable. icon );
- Builder. setMessage (editText. getText ());
- Builder. setTitle (R. string. title );
- DialogInterface. OnClickListener listener = new DialogInterface. OnClickListener (){
- Public void onClick (DialogInterface arg0, int arg1 ){
- // TODO Auto-generated method stub
- RemoveDialog (COMMON_DIALOG );
- }
- };
- Builder. setPositiveButton (R. string. OK, listener );
- Dialog = builder. create ();
- Break;
- Default:
- Break;
- }
- Log. e ("onCreateDialog", "onCreateDialog ");
- Return dialog;
- }
- }
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