Main. xml layout File
<? 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 = "fill_parent">
<EditText android: text = ""
Android: id = "@ + id/editText"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: editable = "false"
Android: cursorVisible = "false"/>
<Button android: text = "show check box dialog box"
Android: id = "@ + id/button"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
</LinearLayout>
Array. xml array
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String-array name = "holobby">
<Item> swimming </item>
<Item> playing basketball </item>
<Item> mountaineering </item>
</String-array>
</Resources>
AlertActivity class
Package com. ljq. dialog;
Import android. app. Activity;
Import android. app. Dialog;
Import android. app. AlertDialog. Builder;
Import android. content. DialogInterface;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
Import android. widget. EditText;
Public class AlertDialog extends Activity {
Private EditText editText;
Private final static int DIALOG = 1;
Boolean [] flags = new boolean [] {false, false, false}; // initial check result
String [] items = null;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Items = getResources (). getStringArray (R. array. holobby );
EditText = (EditText) findViewById (R. id. editText );
Button button = (Button) findViewById (R. id. button );
Button. setOnClickListener (new View. OnClickListener (){
Public void onClick (View v ){
// Display dialog box
ShowDialog (DIALOG );
}
});
}
/**
* Create check box dialog box
*/
@ Override
Protected Dialog onCreateDialog (int id ){
Dialog dialog = null;
Switch (id ){
Case DIALOG:
Builder builder = new android. app. AlertDialog. Builder (this );
// Set the icon in the dialog box
Builder. setIcon (R. drawable. header );
// Set the title of the dialog box
Builder. setTitle ("check box dialog box ");
Builder. setMultiChoiceItems (R. array. holobby, flags, new DialogInterface. OnMultiChoiceClickListener (){
Public void onClick (DialogInterface dialog, int which, boolean isChecked ){
Flags [which] = isChecked;
String result = "you selected :";
For (int I = 0; I <flags. length; I ++ ){
If (flags [I]) {
Result = result + items [I] + ",";
}
}
EditText. setText (result. substring (0, result. length ()-1 ));
}
});
// Add a confirmation button
Builder. setPositiveButton ("OK", new DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog, int which ){
}
});
// Create a check box dialog box
Dialog = builder. create ();
Break;
}
Return dialog;
}
}
Running result