Android在警告对话框中设置了所选项目

And*_*oid 7 android android-alertdialog

我正在我的应用程序开头创建一个警报对话框,让用户选择存储我的应用程序从Web下载的数据的位置.我现在想要实现的是取决于我想要设置所选项之一的内部/外部存储的大小.这是我用来创建对话框的代码:

@SuppressWarnings("static-access")
public void createDialog(){


    final CharSequence[] items = {"Phone Memory - "+memorysize+" free space", "SD Card - "+megAvailable+" MB free space"};

    final int userId = rpc.getUserId(this);
    final String servername = rpc.getCurrentServerName(this);

    SharedPreferences stampiiSettings = PreferenceManager.getDefaultSharedPreferences(MyCollectionList.this);
    final SharedPreferences.Editor editor = stampiiSettings.edit();

    AlertDialog.Builder builder = new AlertDialog.Builder(this.getParent());
    builder.setTitle("Select Storage Path");
    builder.setSingleChoiceItems(items, -1, 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.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
                mHandlerUpdateUi.post(mUpdateUpdateUi); // update UI            
        }
        });
        AlertDialog alert = builder.show();
}
Run Code Online (Sandbox Code Playgroud)

还有一件我想要实现的事情,如果他没有选择任何项目,如何阻止用户关闭警报对话框.我不想在按下后退按钮或单击确定按钮时关闭对话框.欢迎任何想法/建议/帮助!

And*_*oid 19

做这样的事情:

int selected = 0; // or whatever you want
builder.setSingleChoiceItems(items, selected, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
              //onclick
    }});
Run Code Online (Sandbox Code Playgroud)