【问题标题】:Eclipse: Android Soundboard buttons are "overclickable"Eclipse:Android Soundboard 按钮“可过度点击”
【发布时间】:2012-02-27 00:33:31
【问题描述】:

在我的安卓音板中,所有的按钮都可以正常工作,声音也正常播放,但是有一个问题。问题是当你在应用程序中按下一个按钮时,你可以按下其他按钮。例如,如果我按下的按钮发出警报声,我可以按下另一个按钮,两种声音会同时播放。这不是我想要的。有没有我可以添加的代码,这样我一次只能点击一个按钮,或者有一些代码可以让之前的声音停止并播放新选择的声音?

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.soundboard"
  android:versionCode="1"
  android:versionName="1.2">
<application android:icon="@drawable/icon" android:label="@string/app_name"  android:debuggable="false" android:allowClearUserData="true">
    <activity android:label="Vegeta Soundboard" android:screenOrientation="portrait" android:name="Soundboard" android:icon="@drawable/icon">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


</application>

<uses-sdk android:minSdkVersion="3" />


</manifest> 

【问题讨论】:

    标签: android eclipse button click


    【解决方案1】:

    最简单的方法是在用户单击声音按钮时设置一个全局的 soundPlaying 布尔值,并在该声音结束时取消设置。

     public class Main extends Activity{
    
    
    
    
         //global variable, notice how it's not in any method or inner class
        //so it's accessible to all the members of this class
        private boolean soundPlaying = false;
    
    
    
    
         @Override
            public void onCreate(Bundle b){
                super.onCreate(b);
            }
    
    
        //Somewhere in here you have your onclick function that you have called from your xml button
         public void playSound(View v){
        if(soundPlaying)
           return;
        soundPlaying = true;
    //myMedia is your media player object
    
        myMedia.start();//this will only work if you have a media player set up with media
    
        myMedia.setOnCompletionListener(new OnCompletionListener() {            
                @Override
                public void onCompletion(MediaPlayer mp) {
                    soundPlaying = false;//here you set it to false cause the sound is done
    
                }
            });
        }
    
    
    
    
    
        }
    

    【讨论】:

    • 好的,所以我遇到了另一个问题。如何将全局变量合并到 android 应用程序中,以便它在我的手机上运行?
    • 我不确定我理解你的意思。当您使用手机时,您希望看到什么?假设您在您的应用中,您希望电话打进来的效果是什么?
    • 我的意思是如何将全局变量放入应用程序中?我是为它创建一个 JavaScript,我是把它添加到 main.xml,还是放在我没有列出的地方?
    • 只是 Activity 类的顶级布尔成员。
    • 好的,我已经添加了 manifest.xml。我在哪里放置布尔值?
    【解决方案2】:
    public class newBoard extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.main);
    
            Toast.makeText(this, "Thank you for using this App.", Toast.LENGTH_LONG).show();
    
            // ads - load request to display
            AdView layout = (AdView)this.findViewById(R.id.adView);
    
            // ads - load display with an ad
            AdRequest adRequest = new AdRequest();
            adRequest.setTesting(true);
    
            layout.loadAd(adRequest);
    
            // import sound files
            final MediaPlayer sound01 = MediaPlayer.create(this, R.raw.sound01);
            final MediaPlayer sound02 = MediaPlayer.create(this, R.raw.sound02);
            final MediaPlayer sound03 = MediaPlayer.create(this, R.raw.sound03);
            final MediaPlayer sound04 = MediaPlayer.create(this, R.raw.sound04);
            final MediaPlayer sound05 = MediaPlayer.create(this, R.raw.sound05);
            final MediaPlayer sound06 = MediaPlayer.create(this, R.raw.sound06);
            final MediaPlayer sound07 = MediaPlayer.create(this, R.raw.sound07);
            final MediaPlayer sound08 = MediaPlayer.create(this, R.raw.sound08);
            final MediaPlayer sound09 = MediaPlayer.create(this, R.raw.sound09);
            final MediaPlayer sound10 = MediaPlayer.create(this, R.raw.sound10);
            final MediaPlayer sound11 = MediaPlayer.create(this, R.raw.sound11);
            final MediaPlayer sound12 = MediaPlayer.create(this, R.raw.sound12);
            final MediaPlayer sound13 = MediaPlayer.create(this, R.raw.sound13);
            final MediaPlayer sound14 = MediaPlayer.create(this, R.raw.sound14);
            final MediaPlayer sound15 = MediaPlayer.create(this, R.raw.sound15);
            final MediaPlayer sound16 = MediaPlayer.create(this, R.raw.sound16);
            final MediaPlayer sound17 = MediaPlayer.create(this, R.raw.sound17);
            final MediaPlayer sound18 = MediaPlayer.create(this, R.raw.sound18);
            final MediaPlayer sound19 = MediaPlayer.create(this, R.raw.sound19);
            final MediaPlayer sound20 = MediaPlayer.create(this, R.raw.sound20);
            final MediaPlayer sound21 = MediaPlayer.create(this, R.raw.sound21);
            final MediaPlayer sound22 = MediaPlayer.create(this, R.raw.sound22);
            final MediaPlayer sound23 = MediaPlayer.create(this, R.raw.sound23);
            final MediaPlayer sound24 = MediaPlayer.create(this, R.raw.sound24);
            final MediaPlayer sound25 = MediaPlayer.create(this, R.raw.sound25);
    
            // play sound files on clicks
            Button s01 = (Button) findViewById(R.id.button01); 
            s01.setText(this.getString(R.string.quote01));
            s01.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    try {
                        sound01.prepare();
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    sound01.start();                
                    }
    
            });
            registerForContextMenu(s01);
    
            Button s02 = (Button) findViewById(R.id.button02); 
            s02.setText(this.getString(R.string.quote02));
            s02.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    try {
                        sound02.prepare();
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    sound02.start();
                }
            });
            registerForContextMenu(s02);
    
            Button s03 = (Button) findViewById(R.id.button03); 
            s03.setText(this.getString(R.string.quote03));
            s03.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    try {
                        sound03.prepare();
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    sound03.start();
                }
            });
            registerForContextMenu(s03);
    
            Button s04 = (Button) findViewById(R.id.button04); 
            s04.setText(this.getString(R.string.quote04));
            s04.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    try {
                        sound04.prepare();
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    sound04.start();
                }
            });
            registerForContextMenu(s04);
    
            Button s05 = (Button) findViewById(R.id.button05); 
            s05.setText(this.getString(R.string.quote05));
            s05.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    try {
                        sound05.prepare();
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    sound05.start();
                }
            });
            registerForContextMenu(s05);
    
            Button s06 = (Button) findViewById(R.id.button06); 
            s06.setText(this.getString(R.string.quote06));
            s06.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    try {
                        sound06.prepare();
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    sound06.start();
                }
            });
            registerForContextMenu(s06);
    
            Button s07 = (Button) findViewById(R.id.button07); 
            s07.setText(this.getString(R.string.quote07));
            s07.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    try {
                        sound07.prepare();
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    sound07.start();
                }
            });
            registerForContextMenu(s07);
    
            Button s08 = (Button) findViewById(R.id.button08); 
            s08.setText(this.getString(R.string.quote08));
            s08.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    try {
                        sound08.prepare();
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    sound08.start();
                }
            });
            registerForContextMenu(s08);
    
            Button s09 = (Button) findViewById(R.id.button09); 
            s09.setText(this.getString(R.string.quote09));
            s09.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    try {
                        sound09.prepare();
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    sound09.start();
                }
            });
            registerForContextMenu(s09);
    
            Button s10 = (Button) findViewById(R.id.button10); 
            s10.setText(this.getString(R.string.quote10));
            s10.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    try {
                        sound10.prepare();
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    sound10.start();
                }
            });
            registerForContextMenu(s10);
    

    【讨论】:

    • 上面的代码在哪里(你的方向似乎令人困惑)我试图让同样的事情发生(对不起,我是菜鸟
    猜你喜欢
    • 1970-01-01
    • 2020-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多