【问题标题】:Dialog with radio button not works onClick()带有单选按钮的对话框不起作用 onClick()
【发布时间】:2015-02-22 17:43:36
【问题描述】:

我的菜单中有一个项目

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

使用显示带有 3 个单选按钮的警报对话框的方法。当我单击该项目时,对话框会显示,但是当我在对话框中选择某个项目并点击 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)。似乎正按钮不接受选择。有什么问题吗?

【问题讨论】:

  • whichwich 之间有什么区别吗?您在积极按钮点击上使用的
  • 这只是一个打字错误对不起..是一样的真实..

标签: java android radio-button android-alertdialog


【解决方案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);

【讨论】:

  • 是的!现在可以了,谢谢!有没有办法在 sharedpreferences 中保存选择?
  • 当然你可以把它保存在那里。这会给你和想法如何做到这一点。 stackoverflow.com/questions/3624280/…
  • mmh 我不太确定如何在带有单选按钮的对话框中做到这一点。你能告诉我吗?真的非常感谢!
  • mmh 好的,毕竟 if 子句在 onClick 的肯定按钮中,我粘贴代码以存储并在我的 onCreate Activity 中获取代码..但不起作用:(
  • mmh 好的,不用等待,使用我看到它有效的日志.. 它保存了正确的位置,但没有做它在该选项中必须做的事情。不知道你有没有明白我的意思
猜你喜欢
  • 1970-01-01
  • 2015-11-15
  • 2012-10-03
  • 1970-01-01
  • 1970-01-01
  • 2014-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多