为什么其他切换按钮在我的 Onclick 方法中不起作用?

发布于 2025-01-05 08:40:35 字数 3644 浏览 0 评论 0原文

              package com.uygulamalar.androidce;


              import android.app.Activity;
              import android.media.MediaPlayer;
              import android.os.Bundle;
              import android.view.View;
              import android.view.View.OnClickListener;
               import android.widget.ToggleButton;

    public class EglenceliSeslerActivity extends Activity implements OnClickListener {
            /** Called when the activity is first created. */
        ToggleButton togg1,togg2,togg3,togg4,togg5,togg6,togg7,togg8,togg9;
         MediaPlayer sincap,seytanikahkaha,bateri,kalpatisi,trololo,kedicanini,haha,alkis,dedeler;
            @Override
            public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            togg1 = (ToggleButton) findViewById(R.id.togg1);
            togg2 = (ToggleButton) findViewById(R.id.togg2);
            togg3 = (ToggleButton) findViewById(R.id.togg3);
            togg4 = (ToggleButton) findViewById(R.id.togg4);
            togg5 = (ToggleButton) findViewById(R.id.togg5);
            togg6 = (ToggleButton) findViewById(R.id.togg6);
            togg7 = (ToggleButton) findViewById(R.id.togg7);
            togg8 = (ToggleButton) findViewById(R.id.togg8);
            togg9 = (ToggleButton) findViewById(R.id.togg9);

            sincap = MediaPlayer.create(this, R.raw.dramatiksincap);
            seytanikahkaha = MediaPlayer.create(this, R.raw.seytanikahkaha);
            bateri = MediaPlayer.create(this, R.raw.bateri);
            kalpatisi = MediaPlayer.create(this, R.raw.kalpatisi);
            trololo = MediaPlayer.create(this, R.raw.trololo);
            kedicanini = MediaPlayer.create(this, R.raw.kedicanini);
            haha = MediaPlayer.create(this, R.raw.haha);
            alkis = MediaPlayer.create(this, R.raw.alkis);
           dedeler = MediaPlayer.create(this, R.raw.dedeler);

           togg1.setOnClickListener(this);
           togg2.setOnClickListener(this);
           togg3.setOnClickListener(this);
           togg4.setOnClickListener(this);
           togg5.setOnClickListener(this);
           togg6.setOnClickListener(this);
           togg7.setOnClickListener(this);
           togg8.setOnClickListener(this);
           togg9.setOnClickListener(this);

       }
   @Override
    public void onClick(View v) {

    switch(v.getId()) {

    case R.id.togg1:
        if(togg1.isChecked()) {
            sincap.start();
            sincap.setLooping(true);
        }
        else
            sincap.pause();


    case R.id.togg2:
        if(togg2.isChecked()) {
            seytanikahkaha.start();
            seytanikahkaha.setLooping(true);
        }
        else
            seytanikahkaha.pause();

    case R.id.togg3:
        if(togg3.isChecked()) {
            bateri.start();
            bateri.setLooping(true);
        }
        else
            bateri.pause();

    case R.id.togg4:
        if(togg4.isChecked()) {
            kalpatisi.start();
            kalpatisi.setLooping(true);
        }
        else
            kalpatisi.pause();

    case R.id.togg5:
        if(togg5.isChecked()) {
            trololo.start();
            trololo.setLooping(true);
        }
        else
            trololo.pause();

    case R.id.togg6:
        if(togg6.isChecked()) {
            kedicanini.start();
            kedicanini.setLooping(true);
        }
        else
            kedicanini.pause(); 
    }



  }




 }

为什么当我点击其他切换按钮时,除了第一个按钮之外,其他切换按钮不播放音频?第一个切换按钮可以很好地播放音频 sincap,但其他按钮则不能播放音频。你能告诉我为什么吗?谢谢。

              package com.uygulamalar.androidce;


              import android.app.Activity;
              import android.media.MediaPlayer;
              import android.os.Bundle;
              import android.view.View;
              import android.view.View.OnClickListener;
               import android.widget.ToggleButton;

    public class EglenceliSeslerActivity extends Activity implements OnClickListener {
            /** Called when the activity is first created. */
        ToggleButton togg1,togg2,togg3,togg4,togg5,togg6,togg7,togg8,togg9;
         MediaPlayer sincap,seytanikahkaha,bateri,kalpatisi,trololo,kedicanini,haha,alkis,dedeler;
            @Override
            public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            togg1 = (ToggleButton) findViewById(R.id.togg1);
            togg2 = (ToggleButton) findViewById(R.id.togg2);
            togg3 = (ToggleButton) findViewById(R.id.togg3);
            togg4 = (ToggleButton) findViewById(R.id.togg4);
            togg5 = (ToggleButton) findViewById(R.id.togg5);
            togg6 = (ToggleButton) findViewById(R.id.togg6);
            togg7 = (ToggleButton) findViewById(R.id.togg7);
            togg8 = (ToggleButton) findViewById(R.id.togg8);
            togg9 = (ToggleButton) findViewById(R.id.togg9);

            sincap = MediaPlayer.create(this, R.raw.dramatiksincap);
            seytanikahkaha = MediaPlayer.create(this, R.raw.seytanikahkaha);
            bateri = MediaPlayer.create(this, R.raw.bateri);
            kalpatisi = MediaPlayer.create(this, R.raw.kalpatisi);
            trololo = MediaPlayer.create(this, R.raw.trololo);
            kedicanini = MediaPlayer.create(this, R.raw.kedicanini);
            haha = MediaPlayer.create(this, R.raw.haha);
            alkis = MediaPlayer.create(this, R.raw.alkis);
           dedeler = MediaPlayer.create(this, R.raw.dedeler);

           togg1.setOnClickListener(this);
           togg2.setOnClickListener(this);
           togg3.setOnClickListener(this);
           togg4.setOnClickListener(this);
           togg5.setOnClickListener(this);
           togg6.setOnClickListener(this);
           togg7.setOnClickListener(this);
           togg8.setOnClickListener(this);
           togg9.setOnClickListener(this);

       }
   @Override
    public void onClick(View v) {

    switch(v.getId()) {

    case R.id.togg1:
        if(togg1.isChecked()) {
            sincap.start();
            sincap.setLooping(true);
        }
        else
            sincap.pause();


    case R.id.togg2:
        if(togg2.isChecked()) {
            seytanikahkaha.start();
            seytanikahkaha.setLooping(true);
        }
        else
            seytanikahkaha.pause();

    case R.id.togg3:
        if(togg3.isChecked()) {
            bateri.start();
            bateri.setLooping(true);
        }
        else
            bateri.pause();

    case R.id.togg4:
        if(togg4.isChecked()) {
            kalpatisi.start();
            kalpatisi.setLooping(true);
        }
        else
            kalpatisi.pause();

    case R.id.togg5:
        if(togg5.isChecked()) {
            trololo.start();
            trololo.setLooping(true);
        }
        else
            trololo.pause();

    case R.id.togg6:
        if(togg6.isChecked()) {
            kedicanini.start();
            kedicanini.setLooping(true);
        }
        else
            kedicanini.pause(); 
    }



  }




 }

Why the other toggle buttons not playing audios except than first one when i click on them? The first toggle button plays the audio sincap well but the others dom't play the audios. Can you tell me why? Thx.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-残月青衣踏尘吟 2025-01-12 08:40:35

在每个 case 之后,您应该有一个 break 语句,这样您就不会陷入下一个 case。 IE:

...

switch(v.getId()) {

case R.id.togg1:
    if(togg1.isChecked()) {
        sincap.start();
        sincap.setLooping(true);
    }
    else
        sincap.pause();

    break;
case R.id.togg2:
    if(togg2.isChecked()) {
        seytanikahkaha.start();
        seytanikahkaha.setLooping(true);
    }
    else
        seytanikahkaha.pause();
    break;

    ...

After each case you should have a break statement so that you don't fall through to the next case. I.e.:

...

switch(v.getId()) {

case R.id.togg1:
    if(togg1.isChecked()) {
        sincap.start();
        sincap.setLooping(true);
    }
    else
        sincap.pause();

    break;
case R.id.togg2:
    if(togg2.isChecked()) {
        seytanikahkaha.start();
        seytanikahkaha.setLooping(true);
    }
    else
        seytanikahkaha.pause();
    break;

    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文