簡體   English   中英

所有開關盒都在運行

[英]all switch cases are being ran

我正在嘗試編寫一個允許用戶播放聲音的應用程序。 首先,用戶選擇聲音的類別,然后選擇類別中的特定聲音。 一旦選擇了聲音,用戶就可以按下按鈕來播放聲音。

我有3個單選按鈕組,第一個單選按鈕組是類別選擇。 其他2個RadioGroup用於特定的聲音。 我必須將特定的聲音廣播組分成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;
                }
        }
    }

}

更新:我根據給出的所有答案進行了更改。 我只更新了case1:在本文的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);
}

另一個相應,那么它應該工作。

之所以會發出多種聲音,是因為在playSound()方法的外層switch語句中沒有break語句。

您在內部級別switch語句上有break語句,但是需要在它們之前立即添加它們

case 2:

case 3:和情況4:類似。

我也不確定您的if語句if按您期望的方式工作。 您的代碼縮進會提示您的位置:

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

您希望僅在checked為true時才調用所有setChecked方法,但實際上只設置sound1.setChecked(true); 如果checked為true,則將設置所有其他值,而與checked的值無關。 這是因為if僅適用於下一行。 如果您希望它不僅適用於下一行,還需要使用花括號{}來標識if適用的塊。 因此,如果您希望僅在checked情況下將所有設置都設置為true,則需要將其更改為:

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

更新

我只是注意到您在外部switch語句中的每種case也都有兩個switch語句,因此您可能還需要一個標志或其他東西來檢查是否播放了第一個廣播組的聲音,並且僅在不運行第二個switch語句時才運行。

您檢查RadioGroupRadiButton的方式是否選中。 應按以下步驟進行:

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();
}

您的public void playSound (View view){}包含switch語句,而switch語句是switch (type){ case 1:}此switch語句的case不包含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;
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM