保存在AlertDialog中选择的值

时间:2011-07-14 14:13:36

标签: android alertdialog

如何保存所选值?

 private final CharSequence[] mColors = {"1", "2", "3", "4", "5" , "6"};

  switch (id) {
    case w_COLOR:

        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Тест");
        builder.setSingleChoiceItems(mColors, 0, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {

                mResult = item;


            }
        });
        builder.setPositiveButton("Ок", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Toast.makeText(getApplicationContext(), "ITEM: " + mColors[mResult], Toast.LENGTH_LONG).show();
                if (mResult == 0)
                {

                    Parameters params = camera.getParameters();
                    params.setFlashMode(Parameters.FLASH_MODE_ON);
                    camera.setParameters(params);

                    camera.autoFocus(autoFocusCallback);
                }

                if (mResult == 1)
                {

                    Parameters params = camera.getParameters();
                    params.setFlashMode(Parameters.FLASH_MODE_OFF);
                    camera.setParameters(params);

                    camera.autoFocus(autoFocusCallback);

                }
                if (mResult == 2)
                {
                    Parameters params = camera.getParameters();
                    params.setFlashMode(Parameters.FLASH_MODE_ON);
                    camera.setParameters(params);

                }
                if (mResult == 3)
                {
                    Parameters params = camera.getParameters();
                    params.setFlashMode(Parameters.FLASH_MODE_OFF);
                    camera.setParameters(params);

                }

                if (mResult == 4)
                {
                    Parameters params = camera.getParameters();
                    params.setFlashMode(Parameters.FLASH_MODE_TORCH);
                    camera.setParameters(params);
                    camera.autoFocus(autoFocusCallback);
                }

                if (mResult == 5)
                {
                    Parameters params = camera.getParameters();
                    params.setFlashMode(Parameters.FLASH_MODE_TORCH);
                    camera.setParameters(params);

                }

1 个答案:

答案 0 :(得分:0)

设置变量。例如,我在此调用中翻转一个布尔值:

saveDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int whichButton) {
    // Save the record, then flip editMode and change updating
    // editable
    Log.i(TAG, "Save requested. Saving...");
    updateRecord();
    editMode = !editMode;
    titlebar.setText(editMode ? EDIT_TITLE : VIEW_TITLE);
    updateEditable(editMode);
    recordUpdated = false;
    String ems = editMode ? "ON" : "OFF";
    Log.i(TAG, "Switching edit mode "+ems);
    }
}); 

此外,您应该将if结构切换为if...else if... else结构,或者像上面那样切换switch (case)。现在,如果单击setPositiveButton,则会检查每个if语句,并且它们都是互斥的。您也可以将Parameters params = camera.getParameters();拉到if结构之外,因为它似乎每次都被调用。