对话框的多选

public class MultiSelectionDialogExample extends Activity
{
 protected CharSequence[] _options = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };
 protected boolean[] _selections =  new boolean[ _options.length ];
 
 protected Button _optionsButton;
 
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
      
        setContentView(R.layout.main);
       
        _optionsButton = ( Button ) findViewById( R.id.button );
        _optionsButton.setOnClickListener( new ButtonClickHandler()  );
    }
   
    public class ButtonClickHandler implements View.OnClickListener {
  public void onClick( View view ) {
   showDialog( 0 );
  }
 }
   
 @Override
 protected Dialog onCreateDialog( int id )
 {
  return
  new AlertDialog.Builder( this )
         .setTitle( "Planets" )
         .setMultiChoiceItems( _options, _selections, new DialogSelectionClickHandler() )
         .setPositiveButton( "OK", new DialogButtonClickHandler() )
         .create();
 }
 
 
 public class DialogSelectionClickHandler implements DialogInterface.OnMultiChoiceClickListener
 {
  public void onClick( DialogInterface dialog, int clicked, boolean selected )
  {
   Log.i( "ME", _options[ clicked ] + " selected: " + selected );
  }
 }
 

 public class DialogButtonClickHandler implements DialogInterface.OnClickListener
 {
  public void onClick( DialogInterface dialog, int clicked )
  {
   switch( clicked )
   {
    case DialogInterface.BUTTON_POSITIVE:
     printSelectedPlanets();
     break;
   }
  }
 }
 
 protected void printSelectedPlanets(){
  for( int i = 0; i < _options.length; i++ ){
   Log.i( "ME", _options[ i ] + " selected: " + _selections[i] );
  }
 }
}

你可能感兴趣的:(对话框)