Android开警告对话框默认情况下选中的项 [英] android get alert dialog default selected item

查看:143
本文介绍了Android开警告对话框默认情况下选中的项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在设置警报对话框的默认选择的项目有点问题。以下是我在我的code使用:

I have a little problem with setting the default selected item in Alert Dialog. Here is what I use in my code :

if(memory>megAvailable){
        selected = 0;
    } else if(megAvailable>memory){
        selected = 1;
    }

    AlertDialog.Builder builder = new AlertDialog.Builder(this.getParent());
    builder.setTitle("Select Storage Path");
    builder.setSingleChoiceItems(items, selected, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {

            if(item == 0){
                rpc.createFoldersInInternalStorage(servername, userId, MyCollectionList.this);
                Toast.makeText(getApplicationContext(), "Selected Storage Path : Phone Memory", Toast.LENGTH_SHORT).show();
                editor.putInt("storagePath", 1);
                editor.commit();
            } else if (item == 1){
                rpc.createFoldersInExternalStorage(servername, userId, MyCollectionList.this);
                Toast.makeText(getApplicationContext(), "Selected Storage Path : SD Card", Toast.LENGTH_SHORT).show();
                editor.putInt("storagePath", 2);
                editor.commit();
            }
        }});

        builder.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
                mHandlerUpdateUi.post(mUpdateUpdateUi);     
        }
        });
        AlertDialog alert = builder.create();

所以,我现在的问题是我设置所选项目根据一些计算,如果我不选择任何东西,preSS确定,不管我已经在默认情况下它再次创建对话框中选择项目,因为这是主意,如果用户没有选择任何内容。我试着设置项目选择=; 选择=项目; ,但它不工作。我知道我的问题是合乎逻辑的,但我不能弄明白。任何建议如何得到的东西的工作?

So my problem now is I set the selected item depending on some calculations and if i don't select anything and press OK, no matter that I have selected item by default it's creating the dialog again, because that's the idea if user didn't select anything. I've tried to set item=selected; or selected=item; but it's not working. I know my problem is logical , but I can't figure it out. Any suggestions how to get the things to work?

推荐答案

您只需把code,设置在连接到NegativeButton的onClickHandler您storagePath:

You can just put the code that sets your storagePath in the onClickHandler attached to the NegativeButton:

final int defaultSelected = selected +1; //this is final since you need to access it in the anonymous inner class; we're adding 1 since your value that you write seems to be either 1 or 2.
 builder.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
                mHandlerUpdateUi.post(mUpdateUpdateUi);     
                editor.putInt("storagePath", defaultSelected);
                editor.commit();
        }
        });

这篇关于Android开警告对话框默认情况下选中的项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆