如何实现AlertDialog.Builder选中的项目点击事件?

时间:2013-03-06 03:06:59

标签: java android android-alertdialog android-event

我想实现AlertDialog.Builder个所选项目点击事件。以下是我到目前为止所尝试的内容。我对Android很新,我不知道如何访问该事件。如何为列表中的每个项目实现click事件?

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;

public class MakeCallAlertDialog {

    public static AlertDialog.Builder getAlertDialog(String strArray[],
            String strTitle, Activity activity) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
        alertDialogBuilder.setTitle(strTitle);
        alertDialogBuilder.setItems(strArray, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialogInterface, int arg) {
                // TODO Auto-generated method stub
            }
        });

        return alertDialogBuilder;
    }
}

3 个答案:

答案 0 :(得分:8)

由于您为该方法指定了OnClickListener,因此int参数为position in the list

  

参数

     

对话框收到点击的对话框。

     

点击的按钮(例如BUTTON1)点击该项目的位置

这意味着在您的方法中,您应该能够这样做:

public static AlertDialog.Builder getAlertDialog(final String strArray[],
        String strTitle, final Activity activity) {

    AlertDialog.Builder alertDialogBuilder =  
            new AlertDialog.Builder(activity);
    alertDialogBuilder.setTitle(strTitle);

    alertDialogBuilder.setItems(strArray,
            new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
           Toast.makeText(activity, strArray [which], Toast.LENGTH_SHORT).show();

           //rest of your implementation
        }
    });
   return alertDialogBuilder;
}

答案 1 :(得分:5)

在onClick()事件中使用switch语句为每个按钮编写click方法。

    @Override
        public void onClick(DialogInterface dialogInterface, int arg) {
            // TODO Auto-generated method stub
            switch (arg) {
               case 0:
                  //you code for button at 0 index click
                  break;
               case 1:
                  //you code for button at 1 index click
                  break;
               default:
                    break;
            }
        }

这里,arg表示按下按钮的索引。你也可以使用strArray [arg]

访问该按钮

答案 2 :(得分:1)

如果您使用为strArray选择的单项选项,请查看下面的答案:尝试此代码

int selectedItem = 0;

//这里采取TempSelectOneTypeList = strArray

 AlertDialog.Builder alt_bld = new AlertDialog.Builder(
                                    Activity_Form_Data.this);
                            alt_bld.setTitle("Select One");
                            selectedItem = 0;
                            for (int j = 0; j < TempSelectOneTypeList.length; j++) {

                                if (txt_sub_lable2
                                        .getText()
                                        .toString()
                                        .equals(TempSelectOneTypeList[j].toString())) {
                                    selectedItem = j;
                                }
                            }

                            Log.i(TAG, "Selected Item is " + selectedItem);

                            alt_bld.setSingleChoiceItems(
                                    ArraylistSelectOneTypeList.get(selected),
                                    selectedItem,
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog,
                                                int item) {
                                            selectedItem = item;
                                            // you can ocde here for the perticular selected item
                                        }
                                    });
                            alt_bld.setPositiveButton("OK",
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog,
                                                int which) {

                                            txt_sub_lable2
                                                    .setText(""
                                                            + TempSelectOneTypeList[selectedItem]
                                                                    .toString());
    }
                                    });
                            alt_bld.setNegativeButton("Cancel",
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog,
                                                int which) {
                                            dialog.dismiss();

                                        }
                                    });

                            AlertDialog alert = alt_bld.create();
                            alert.show();

希望它能解决你的问题