带单选按钮的对话框不适用onClick()

时间:2014-12-24 16:29:30

标签: java android radio-button android-alertdialog

我的菜单中有一个项目

case R.id.theme:
   ShowRadioDialog();           
 return true;

使用显示带3个单选按钮的Alert对话框的方法。当我点击对话框显示的项目,但当我在对话框中选择一些项目时,我点击positiove按钮没有任何反应。这是方法:

public void ShowRadioDialog() {
        final CharSequence[] items={"Rosso","Verde","Blu"};
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("Seleziona un colore");
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

            @SuppressLint("NewApi")
            @Override
            public void onClick(DialogInterface dialog, int which) {

            if (wich== 1) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                            getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.red));
                            toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.red));
                            Toast.makeText(MainActivity.this, "Rosso OK", Toast.LENGTH_SHORT).show();
                            Log.i("Colors", "Rosso Ok");
                        }
                } else if (wich ==2) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.green_welcome));
                    }
                } else if (wich == 3){
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.primary_dark_blue));
                    }
                }
            }
        });

        builder.setSingleChoiceItems(items,-1, new DialogInterface.OnClickListener() {

            @SuppressLint("NewApi")
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

                    if ("Rosso".equals(items[which])) {
                        which = 1;
                    } else if ("Verde".equals(items[which])) {

                        which = 2;
                    } else if ("Blu".equals(items[which])) {

                        which = 3;
                    }

            }
        });
        builder.show();
    }

我不确定这是正确的方法。但是,不会出现任何一个(登录logcat和应用程序中的Toast)。似乎正面按钮不接受选择。出了什么问题?

1 个答案:

答案 0 :(得分:1)

在顶层定义变量并使用它来访问所选项目。

int index = -1;

public void ShowRadioDialog() {
    final CharSequence[] items={"Rosso","Verde","Blu"};
    AlertDialog.Builder builder=new AlertDialog.Builder(this);
    builder.setTitle("Seleziona un colore");
    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

        @SuppressLint("NewApi")
        @Override
        public void onClick(DialogInterface dialog, int which) {

        if (index == 1) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.red));
                        toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.red));
                        Toast.makeText(MainActivity.this, "Rosso OK", Toast.LENGTH_SHORT).show();
                        Log.i("Colors", "Rosso Ok");
                    }
            } else if (index ==2) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.green_welcome));
                }
            } else if (index == 3){
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.primary_dark_blue));
                }
            }
        }
    });

    builder.setSingleChoiceItems(items,-1, new DialogInterface.OnClickListener() {

        @SuppressLint("NewApi")
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

                if ("Rosso".equals(items[which])) {
                    index = 1;
                } else if ("Verde".equals(items[which])) {

                    index = 2;
                } else if ("Blu".equals(items[which])) {

                    index = 3;
                }

        }
    });
    builder.show();
}

如果您想在点击“确定”按钮时保存共享首选项中index的值,请执行以下操作

存储在SharedPreferences

SharedPreferences preferences = getSharedPreferences("myPref", getApplicationContext().MODE_PRIVATE);
Editor editor = preferences.edit();
editor.putInt("choice", index);
editor.commit();

在需要时从SharedPreferences获取

SharedPreferences preferences = getSharedPreferences("myPref", getApplicationContext().MODE_PRIVATE);
int index = preferences.getInt("choice",-1);