单选按钮警告对话框返回值

时间:2015-11-13 05:40:29

标签: android radio-button return

在我的活动中,我想创建一个弹出警告对话框,其中包含几个选项的单选按钮,我希望每个按钮在警告框中按确定时返回一些整数值。我该如何实现这个。请帮助。

long a;
    final CharSequence[] items = {" 20min before", " 40min before", " 60min before", " 1hour 30min before"};

    AlertDialog.Builder builder = new AlertDialog.Builder(MovieDetailActivity.this);
    builder.setTitle("Set Reminder For this movie");
    builder.setIcon(R.drawable.ic_action_time);
    builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();


            if (items[item].toString().equals("20min before")) {
                a = 1 * 60 * 1000;
                Toast.makeText(getApplicationContext(), "In 1st", Toast.LENGTH_SHORT).show();

            }
            if (items[item].toString().equals("40min before")) {
                a = 40 * 60 * 1000;
                Toast.makeText(getApplicationContext(), "In 2nd", Toast.LENGTH_SHORT).show();
            }
            if (items[item].toString().equals("60min before")) {
                a = 60 * 60 * 1000;
            }
            if (items[item].toString().equals("90min before")) {
                a = 90 * 60 * 1000;

            }

        }

    });

1 个答案:

答案 0 :(得分:0)

equalsIgnoreCase用于比较Stringtrim用于删除空格。

            if (items[item].toString().trim().equalsIgnoreCase("20min before")) {
                a = 1 * 60 * 1000;
                Toast.makeText(getApplicationContext(), "In 1st", Toast.LENGTH_SHORT).show();

            }else if (items[item].toString().trim().equalsIgnoreCase("40min before")) {
                a = 40 * 60 * 1000;
                Toast.makeText(getApplicationContext(), "In 2nd", Toast.LENGTH_SHORT).show();
            }else if (items[item].toString().trim().equalsIgnoreCase("60min before")) {
                a = 60 * 60 * 1000;
            }else(items[item].toString().trim().equalsIgnoreCase("90min before")) {
                a = 90 * 60 * 1000;

            }