java 如何实现 AlertDialog.Builder 选定项单击事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15238245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:56:58  来源:igfitidea点击:

How to implement an AlertDialog.Builder selected item click event?

javaandroidandroid-alertdialogandroid-event

提问by co2f2e

I want to implement AlertDialog.Builderselected items click event. Below is what I have tried so far. I'm quite new to Android and I'm not sure how to access that event. How to implement the click event for each individual item in the list?

我想实现AlertDialog.Builder选定项目的点击事件。以下是我迄今为止尝试过的。我对 Android 很陌生,我不确定如何访问该事件。如何为列表中的每个单独的项目实现点击事件?

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

回答by A--C

Since you assigned an OnClickListenerspecific to that method, the intparameter is the position in the list:

由于您OnClickListener为该方法分配了特定内容,因此int参数是列表中位置

Parameters

dialogThe dialog that received the click.

whichThe button that was clicked (e.g. BUTTON1) orthe position of the item clicked

参数

dialog收到点击的对话框。

which被点击的按钮(例如 BUTTON1)或被点击的项目的位置

This means inside your method, you should be able to do this:

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

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

回答by Akbari Dipali

in onClick() event use switch statement to write click method for each button.

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

    @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;
            }
        }

Here, arg indicates the index of the button pressed. you can also access that button using strArray[arg]

这里,arg 表示按下的按钮的索引。您还可以使用 strArray[arg] 访问该按钮

回答by Maulik

Check my answer below if you are using single choice item selected for the strArray: Try this code

如果您使用为 strArray 选择的单项选择项,请检查下面我的回答:试试这个代码

int selectedItem = 0;

// here take TempSelectOneTypeList = strArray

// 这里取 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();

Hope it will solve your problem

希望它能解决你的问题