对话框的多选

本文展示了一个Android应用中实现的多选对话框示例,该对话框允许用户从一系列选项(例如行星名称)中进行选择,并记录用户的选取结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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] );
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值