2 u010025993 u010025993 于 2013.08.14 17:15 提问

在DialogBox数组中获取值

在数组中获取选中项目的值,代码:

 final String items[] = {"Blue","Green","Orange"};
final AlertDialog.Builder alert = new AlertDialog.Builder(this);  
alert.setTitle("Money Maker");
alert.setIcon(R.drawable.ic_launcher);
alert.setMultiChoiceItems(items, checkedItems,  new 
DialogInterface.OnClickListener() {
    @Override
    public void onClick(final DialogInterface dialog, final int which) {
        String value = items.toString();
        Toast.makeText(getApplication(), value, Toast.LENGTH_LONG).show();
    }
});
alert.show();

怎么获取值并且在toast中显示?谢谢。

2个回答

Jave_
Jave_   2013.08.14 17:28
已采纳

应该是:

alert.setMultiChoiceItems(items, checkedItems,  new 
OnMultiChoiceClickListener() {
    @Override
    public void onClick(DialogInterface dialog,
                                   int which, boolean isChecked) {
        Toast.makeText(getApplication(), items[which], Toast.LENGTH_LONG).show();
    }
});
loFataMer
loFataMer   2013.08.14 17:45
  //store all selected items in arraylist
 ArrayList<String> selectedItems=new ArrayList<String>();
 final CharSequence[] items = { "Gujrat", "Rajasthan",
                    "Maharastra", "Panjab", "Madhya Pradesh", "Hariyana",
                    "Bihar" };
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle("Select State");

            builder.setMultiChoiceItems(items, null,
                    new DialogInterface.OnMultiChoiceClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                int which, boolean isChecked) {
                            // TODO Auto-generated method stub
                            if (isChecked){
                                Toast.makeText(getApplicationContext(),
                                        items[which], Toast.LENGTH_SHORT)
                                        .show();
                              selectedItems.add(items[which]);
                           }else{
                                   if(selectedItems.contains(items[which])
                                          selectedItems.remove(items[which]);
                           }
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();
        }
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!