所有开关盒都在 运行

all switch cases are being ran

我正在尝试编写一个允许用户播放声音的应用程序。首先,用户选择声音类别,然后选择类别中的特定声音。选择声音后,用户按下按钮播放声音。

我有3个单选按钮组,第一个单选按钮组是类别选择。其他 2 个 RadioGroups 用于特定的声音。我不得不将特定的声音无线电组分成 2 个,因为这是我可以将其放入屏幕的唯一方法。 Switch 语句用于确定选择了哪个类别和哪个特定声音。所有无线电组都在工作,播放声音的按钮也是如此。

问题在于它正在播放来自多个 switch 语句的多个声音。任何有关如何解决该问题的提示都将不胜感激。

public class Main extends ActionBarActivity {
    private RadioGroup category, topRow, bottomRow;
    private RadioButton animal, people, crashes, explosions, sound1, sound2, sound3, sound4, sound5, sound6;
    private Button player;
    int type;
boolean topPlayed = false;
boolean bottomPlayed = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_punkd_main);

        //UI elements
        //Radio button groups
        category = (RadioGroup) findViewById(R.id.soundType);
        topRow = (RadioGroup) findViewById(R.id.topthree);
        bottomRow = (RadioGroup) findViewById(R.id.bottomthree);
        //Radio buttons
        //group category
        animal = (RadioButton) findViewById(R.id.Animals);
        people = (RadioButton) findViewById(R.id.People);
        crashes = (RadioButton) findViewById(R.id.Crashes);
        explosions = (RadioButton) findViewById(R.id.Explosions);
        //group topRow
        sound1 = (RadioButton) findViewById(R.id.Sound1);
        sound2 = (RadioButton) findViewById(R.id.Sound2);
        sound3 = (RadioButton) findViewById(R.id.Sound3);
        //group bottomRow
        sound4 = (RadioButton) findViewById(R.id.Sound4);
        sound5 = (RadioButton) findViewById(R.id.Sound5);
        sound6 = (RadioButton) findViewById(R.id.Sound6);
        // the button
        player = (Button) findViewById(R.id.Player);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // not needed here
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // not needed here
    }
    //select which type of sound to use, set text on hidden radio buttons, and make them visible
    public void categorySelect(View view){
        boolean checked = ((RadioButton) view).isChecked();

        switch (view.getId()){

            case R.id.Animals:
                if (checked)
                    {type = 1;
                    topRow.setVisibility(View.VISIBLE);
                    bottomRow.setVisibility(View.VISIBLE);
                sound5.setVisibility(View.VISIBLE);//some options rehide these
                sound6.setVisibility(View.VISIBLE);

                    sound1.setText("noise1");
                    sound2.setText("noise2");
                    sound3.setText("noise3");
                    sound4.setText("noise4");
                    sound5.setText("noise5");
                    sound6.setText("noise6");}
                break;

            case R.id.People:
                if (checked)
                    {type = 2;
                    topRow.setVisibility(View.VISIBLE);
                bottomRow.setVisibility(View.VISIBLE);
                sound5.setVisibility(View.VISIBLE);//some options rehide these
                sound6.setVisibility(View.VISIBLE);

                sound1.setText("noise1");
                sound2.setText("noise2");
                sound3.setText("noise3");
                sound4.setText("noise4");
                sound5.setText("noise5");
                sound6.setText("noise6");}
                break;

            case R.id.Crashes:
                if (checked)
                    {type = 3;
                    topRow.setVisibility(View.VISIBLE);
                bottomRow.setVisibility(View.VISIBLE);
                sound6.setVisibility(View.INVISIBLE);

                sound1.setText("noise1");
                sound2.setText("noise2");
                sound3.setText("noise3");
                sound4.setText("noise4");
                sound5.setText("noise5");
                sound6.setText("error");}//should not be seen in this category

                break;

            case R.id.Explosions:
                if (checked)
                    {type = 4;
                    topRow.setVisibility(View.VISIBLE);
                bottomRow.setVisibility(View.VISIBLE);
                sound5.setVisibility(View.INVISIBLE);
                sound6.setVisibility(View.INVISIBLE);

                sound1.setText("noise1");
                sound2.setText("noise2");
                sound3.setText("noise3");
                sound4.setText("noise4");
                sound5.setText("error");//should not be seen
                sound6.setText("error");}//should not be seen

                break;
        }

    }
    //used to make 2 different radioGroups work as 1
    public void soundSelect(View view){

        boolean checked = ((RadioButton) view).isChecked();

        switch (view.getId()){

            case R.id.Sound1:
             if (checked)
{topPlayed = true;
bottomPlayed = false;
                    sound1.setChecked(true);
                    sound2.setChecked(false);
                    sound3.setChecked(false);
                    sound4.setChecked(false);
                    sound5.setChecked(false);
                    sound6.setChecked(false);}
                break;

            case R.id.Sound2:
             if (checked)
{topPlayed = true;
bottomPlayed = false;
                    sound1.setChecked(false);
                    sound2.setChecked(true);
                    sound3.setChecked(false);
                    sound4.setChecked(false);
                    sound5.setChecked(false);
                    sound6.setChecked(false);}
                break;
            case R.id.Sound3:
            if (checked)
{topPlayed = true;
bottomPlayed = false;
                    sound1.setChecked(false);
                sound2.setChecked(false);
                sound3.setChecked(true);
                sound4.setChecked(false);
                sound5.setChecked(false);
                sound6.setChecked(false);}
                break;
            case R.id.Sound4:
             if (checked)
{topPlayed = false;
bottomPlayed = true;
                    sound1.setChecked(false);
                sound2.setChecked(false);
                sound3.setChecked(false);
                sound4.setChecked(true);
                sound5.setChecked(false);
                sound6.setChecked(false);}
                break;
            case R.id.Sound5:
            if (checked)
{topPlayed = false;
bottomPlayed = true;
                sound1.setChecked(false);
                sound2.setChecked(false);
                sound3.setChecked(false);
                sound4.setChecked(false);
                sound5.setChecked(true);
                sound6.setChecked(false);}
                break;
            case R.id.Sound6:
             if (checked)
{topPlayed = false;
bottomPlayed = true;
                 sound1.setChecked(false);
                 sound2.setChecked(false);
                 sound3.setChecked(false);
                 sound4.setChecked(false);
                 sound5.setChecked(false);
                 sound6.setChecked(true);}
                break;
        }

    }

    public void playSound (View view){
        String msg;

        /* Was used to test if the type variable was setting right. commented out cause it works- delete before going live
        String msg2 = type;
        Toast.makeText(getApplicationContext(), msg2, Toast.LENGTH_LONG).show();*/


        switch (type){
            case 1:
                switch (topRow.getCheckedRadioButtonId()) {

                    case R.id.Sound1:

if (topPlayed){msg = "animal first sound";
                            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                            MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                            mp.start();}
                        break;

                    case R.id.Sound2:
if (topPlayed)
                        {msg = "animal second sound";
                            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                            MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                            mp2.start();}
                        break;

                    case R.id.Sound3:
if (topPlayed)
                        {msg = "animal third sound";
                            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                            MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sound);
                            mp3.start();}
                        break;
                }
                    switch (bottomRow.getCheckedRadioButtonId()){

                    case R.id.Sound4:
if (bottomPlayed)
                        {msg = "animal fourth sound";
                            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                            MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                            mp.start();}
                        break;

                    case R.id.Sound5:
if (bottomPlayed)
                        {msg = "animal fifth sound";
                            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                            MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                            mp2.start();}
                        break;

                    case R.id.Sound6:
if (bottomPlayed)
                        {msg = "animal sixth sound";
                            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                            MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sound);
                            mp3.start();}
                        break;
                }

            case 2:
                switch (topRow.getCheckedRadioButtonId()){

                    case R.id.Sound1:
                    msg = "person first sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();
                        break;

                    case R.id.Sound2:
                        msg = "person second sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                        mp2.start();
                        break;

                    case R.id.Sound3:
                        msg = "person third sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                     MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sound);
                        mp3.start();
                        break;
                }

                switch (bottomRow.getCheckedRadioButtonId()){
                    case R.id.Sound4:
                        msg = "person fourth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();
                        break;

                    case R.id.Sound5:
                        msg = "person fifth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                        mp2.start();
                        break;

                    case R.id.Sound6:
                        msg = "person sixth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sound);
                        mp3.start();
                        break;
                }

            case 3:
                switch (topRow.getCheckedRadioButtonId()) {

                    case R.id.Sound1:
                    msg = "crash first sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();
                        break;

                    case R.id.Sound2:
                        msg = "crash second sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                        mp2.start();
                        break;

                    case R.id.Sound3:
                        msg = "crash third sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sound);
                        mp3.start();
                        break;
                }

                switch (bottomRow.getCheckedRadioButtonId()){

                    case R.id.Sound4:
                        msg = "crash fourth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();
                        break;

                    case R.id.Sound5:
                        msg = "crash fifth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                        mp2.start();
                        break;

                    case R.id.Sound6:
                        msg = "should not see this option";
                        Toast.makeText (getApplicationContext(), msg, Toast.LENGTH_SHORT).show ();

                        break;

                }

            case 4:
                switch (topRow.getCheckedRadioButtonId()) {

                    case R.id.Sound1:
                    msg = "explode first sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();
                        break;

                    case R.id.Sound2:
                        msg = "explode second sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    {MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                        mp2.start();
                        break;

                    case R.id.Sound3:
                        msg = "explode third sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sound);
                        mp3.start();
                        break;
                }
                switch (bottomRow.getCheckedRadioButtonId()){

                    case R.id.Sound4:
                        msg = "explode fourth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();

                        break;

                    case R.id.Sound5:
                        msg = "should not see this option";
                        Toast.makeText (getApplicationContext(), msg, Toast.LENGTH_SHORT).show ();
                        break;

                    case R.id.Sound6:
                        msg = "should not see this option";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        break;
                }
        }
    }

}

更新: 我根据给出的所有答案进行了更改。我只在这个 post 中更新了 case1:in playSound(),在应用程序中,每个案例都得到了更新,应用程序现在可以按预期运行。感谢您的帮助!

这是java,不能仅通过缩进来标记块。 所以,在你的 if 块周围放置花边通常是个好主意,例如:

改变

if (checked)
                sound1.setChecked(true);
                sound2.setChecked(false);
                sound3.setChecked(false);
                sound4.setChecked(false);
                sound5.setChecked(false);
                sound6.setChecked(false);

if (checked) {
                sound1.setChecked(true);
                sound2.setChecked(false);
                sound3.setChecked(false);
                sound4.setChecked(false);
                sound5.setChecked(false);
                sound6.setChecked(false);
}

和另一个相应的,那么它应该可以工作。

您在 RadioGroup 中检查 RadiButton 的方式是否符合您的检查方式。应该按如下方式进行:

if(gender.getCheckedRadioButtonId()==-1)
{
    Toast.makeText(getApplicationContext(), "Please select Gender", Toast.LENGTH_SHORT).show();
}
else
{
    // get selected radio button from radioGroup
    int selectedId = gender.getCheckedRadioButtonId();
    // find the radiobutton by returned id
    selectedRadioButton = (RadioButton)findViewById(selectedId);
    Toast.makeText(getApplicationContext(), selectedRadioButton.getText().toString()+" is selected", Toast.LENGTH_SHORT).show();
}

您收到多个声音的原因是您在 playSound() 方法的外层 switch 语句中没有 break 语句。

您在 switch 语句的内层有 break 语句,但您需要在

之前立即添加它们
case 2:

并且对于 case 3: 和情况 4 类似:`.

我也不确定您的 if 语句是否按照您期望的方式工作。您的代码缩进表明您的位置:

if (checked)
    sound1.setChecked(true);
    sound2.setChecked(false);
    sound3.setChecked(false);
    sound4.setChecked(false);
    sound5.setChecked(false);
    sound6.setChecked(false);

您希望仅当 checked 为真时才调用所有 setChecked 方法,但实际上它只会在 checked 为真时设置 sound1.setChecked(true);并且不管 checked 的值如何,都会设置所有其他的。这是因为 if 仅适用于下一行。如果您希望它不仅仅应用于下一行,则需要使用大括号 {} 来标识 if 适用的块。所以,如果你想让它只在 checked 为真时设置所有这些,你需要将它更改为:

if (checked) {
    sound1.setChecked(true);
    sound2.setChecked(false);
    sound3.setChecked(false);
    sound4.setChecked(false);
    sound5.setChecked(false);
    sound6.setChecked(false);
}

更新

我刚刚注意到您在外部 switch 语句的每个 case 中也有两个 switch 语句,因此您可能还需要一个标志或其他东西来检查您是否播放了声音来自您的第一个广播组,如果您没有,则仅 运行 第二个 switch 声明。

您的 public void playSound (View view){} 包含 switch 语句,即 switch (type){ case 1:} 此 switch 语句不包含导致播放多个声音的 break; 语句。将您的方法更改为如下所示,您的代码将像魅力一样工作。

public void playSound (View view){
        String msg;

        /* Was used to test if the type variable was setting right. commented out cause it works- delete before going live
        String msg2 = type;
        Toast.makeText(getApplicationContext(), msg2, Toast.LENGTH_LONG).show();*/


        switch (type){
            case 1:
                switch (topRow.getCheckedRadioButtonId()) {

                    case R.id.Sound1:
                     msg = "animal first sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();
                        break;

                    case R.id.Sound2:
                    msg = "animal second sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                        mp2.start();
                        break;

                    case R.id.Sound3:
                    msg = "animal third sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sound);
                        mp3.start();
                        break;
                }
                    switch (bottomRow.getCheckedRadioButtonId()){

                    case R.id.Sound4:
                    msg = "animal fourth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();
                        break;

                    case R.id.Sound5:
                    msg = "animal fifth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                        mp2.start();
                        break;

                    case R.id.Sound6:
                    msg = "animal sixth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sound);
                        mp3.start();
                        break;
                }
              break;
            case 2:
                switch (topRow.getCheckedRadioButtonId()){

                    case R.id.Sound1:
                    msg = "person first sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();
                        break;

                    case R.id.Sound2:
                        msg = "person second sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                        mp2.start();
                        break;

                    case R.id.Sound3:
                        msg = "person third sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                     MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sound);
                        mp3.start();
                        break;
                }

                switch (bottomRow.getCheckedRadioButtonId()){
                    case R.id.Sound4:
                        msg = "person fourth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();
                        break;

                    case R.id.Sound5:
                        msg = "person fifth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                        mp2.start();
                        break;

                    case R.id.Sound6:
                        msg = "person sixth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sound);
                        mp3.start();
                        break;
                }
            break;
            case 3:
                switch (topRow.getCheckedRadioButtonId()) {

                    case R.id.Sound1:
                    msg = "crash first sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();
                        break;

                    case R.id.Sound2:
                        msg = "crash second sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                        mp2.start();
                        break;

                    case R.id.Sound3:
                        msg = "crash third sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sound);
                        mp3.start();
                        break;
                }

                switch (bottomRow.getCheckedRadioButtonId()){

                    case R.id.Sound4:
                        msg = "crash fourth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();
                        break;

                    case R.id.Sound5:
                        msg = "crash fifth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                        mp2.start();
                        break;

                    case R.id.Sound6:
                        msg = "should not see this option";
                        Toast.makeText (getApplicationContext(), msg, Toast.LENGTH_SHORT).show ();

                        break;

                }
           break;
            case 4:
                switch (topRow.getCheckedRadioButtonId()) {

                    case R.id.Sound1:
                    msg = "explode first sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();
                        break;

                    case R.id.Sound2:
                        msg = "explode second sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    {MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                        mp2.start();
                        break;

                    case R.id.Sound3:
                        msg = "explode third sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sound);
                        mp3.start();
                        break;
                }
                switch (bottomRow.getCheckedRadioButtonId()){

                    case R.id.Sound4:
                        msg = "explode fourth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();

                        break;

                    case R.id.Sound5:
                        msg = "should not see this option";
                        Toast.makeText (getApplicationContext(), msg, Toast.LENGTH_SHORT).show ();
                        break;

                    case R.id.Sound6:
                        msg = "should not see this option";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        break;
                }
           break;
        }
    }