保存在 AlertDialog 中选择的值

发布于 2024-11-23 16:35:24 字数 2432 浏览 0 评论 0原文

如何保存选定的值?

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

                }

How do I save the selected value?

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

                }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不咬妳我踢妳 2024-11-30 16:35:24

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

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 结构之外,因为它似乎每次都会被调用。

Set a variable. For example I flip a boolean value in this call:

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

Also, you should probably either switch the if structure to an if...else if... else structure or switch (case) like you do above. As it is now if that setPositiveButton is clicked you're checking each if statement and they're all mutually exclusive. You can also probably pull the Parameters params = camera.getParameters(); outside of the if structure since it seems to be called each time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文