2014-09-29 98 views
1

我正在研究將有15個按鈕的應用程序。每個按鈕都有不同的聲音。我希望按下按鈕播放聲音,當手指擡起停止聲音並且工作良好時,LogCat中不顯示任何延遲和錯誤/警告。按下按鈕時,手指在按鈕視圖中,android OnTouchListener

但是之後我在屏幕規格不佳的設備上測試了我的應用(設備沒有多點觸控),我注意到只有1個按鈕可以按下,直到手指擡起。

所以我需要的東西是這樣的:

當手指Button1的邊框內,按鈕1將被壓。當手指移動並離開按鈕1邊框時,按鈕1將不再被按下,當手指進入按鈕2或按鈕4邊框或任何其他按鈕時,該按鈕將被按下。所以我需要激活手指所在的按鈕。 但用我的代碼,即使在手指離開按鈕1邊框後,按鈕1仍然被按下。 這裏是圖片: enter image description here

這裏是我的XML佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/pad_background_selector" > 

    <RelativeLayout 
     android:id="@+id/relativeLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:background="@drawable/view" > 

     <ImageButton 
      android:id="@+id/record_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:background="@null" 
      android:src="@drawable/static_selector_record" /> 

     <CheckBox 
      android:id="@+id/checkBox1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentTop="true" /> 

     <ImageButton 
      android:id="@+id/info_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true" 
      android:background="@null" 
      android:src="@drawable/static_selector_info" /> 
    </RelativeLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@+id/adspace" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/relativeLayout1" 
     android:baselineAligned="false" 
     android:orientation="horizontal" 
     android:weightSum="3" > 

     <LinearLayout 
      android:id="@+id/first" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="vertical" 
      android:weightSum="5" > 

      <Button 
       android:id="@+id/sound1" 
       android:layout_width="match_parent" 
       android:layout_height="fill_parent" 
       android:layout_margin="4.8dp" 
       android:layout_weight="1" 
       android:background="@drawable/static_pad_selector" 
       tools:ignore="NestedWeights" /> 

      <Button 
       android:id="@+id/sound4" 
       android:layout_width="match_parent" 
       android:layout_height="fill_parent" 
       android:layout_margin="4.8dp" 
       android:layout_weight="1" 
       android:background="@drawable/static_pad_selector" /> 

      <Button 
       android:id="@+id/sound7" 
       android:layout_width="match_parent" 
       android:layout_height="fill_parent" 
       android:layout_margin="4.8dp" 
       android:layout_weight="1" 
       android:background="@drawable/static_pad_selector" /> 

      <Button 
       android:id="@+id/sound10" 
       android:layout_width="match_parent" 
       android:layout_height="fill_parent" 
       android:layout_margin="4.8dp" 
       android:layout_weight="1" 
       android:background="@drawable/static_pad_selector" /> 

      <Button 
       android:id="@+id/sound13" 
       android:layout_width="match_parent" 
       android:layout_height="fill_parent" 
       android:layout_margin="4.8dp" 
       android:layout_weight="1" 
       android:background="@drawable/static_pad_selector" /> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/third" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="vertical" 
      android:weightSum="5" > 

      <Button 
       android:id="@+id/sound2" 
       android:layout_width="match_parent" 
       android:layout_height="fill_parent" 
       android:layout_margin="4.8dp" 
       android:layout_weight="1" 
       android:background="@drawable/static_pad_selector" 
       tools:ignore="NestedWeights" /> 

      <Button 
       android:id="@+id/sound5" 
       android:layout_width="match_parent" 
       android:layout_height="fill_parent" 
       android:layout_margin="4.8dp" 
       android:layout_weight="1" 
       android:background="@drawable/static_pad_selector" /> 

      <Button 
       android:id="@+id/sound8" 
       android:layout_width="match_parent" 
       android:layout_height="fill_parent" 
       android:layout_margin="4.8dp" 
       android:layout_weight="1" 
       android:background="@drawable/static_pad_selector" /> 

      <Button 
       android:id="@+id/sound11" 
       android:layout_width="match_parent" 
       android:layout_height="fill_parent" 
       android:layout_margin="4.8dp" 
       android:layout_weight="1" 
       android:background="@drawable/static_pad_selector" /> 

      <Button 
       android:id="@+id/sound14" 
       android:layout_width="match_parent" 
       android:layout_height="fill_parent" 
       android:layout_margin="4.8dp" 
       android:layout_weight="1" 
       android:background="@drawable/static_pad_selector" /> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/second" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="vertical" 
      android:weightSum="5" > 

      <Button 
       android:id="@+id/sound3" 
       android:layout_width="match_parent" 
       android:layout_height="fill_parent" 
       android:layout_margin="4.8dp" 
       android:layout_weight="1" 
       android:background="@drawable/static_pad_selector" 
       tools:ignore="NestedWeights" /> 

      <Button 
       android:id="@+id/sound6" 
       android:layout_width="match_parent" 
       android:layout_height="fill_parent" 
       android:layout_margin="4.8dp" 
       android:layout_weight="1" 
       android:background="@drawable/static_pad_selector" /> 

      <Button 
       android:id="@+id/sound9" 
       android:layout_width="match_parent" 
       android:layout_height="fill_parent" 
       android:layout_margin="4.8dp" 
       android:layout_weight="1" 
       android:background="@drawable/static_pad_selector" /> 

      <Button 
       android:id="@+id/sound12" 
       android:layout_width="match_parent" 
       android:layout_height="fill_parent" 
       android:layout_margin="4.8dp" 
       android:layout_weight="1" 
       android:background="@drawable/static_pad_selector" /> 

      <Button 
       android:id="@+id/sound15" 
       android:layout_width="match_parent" 
       android:layout_height="fill_parent" 
       android:layout_margin="4.8dp" 
       android:layout_weight="1" 
       android:background="@drawable/static_pad_selector" /> 
     </LinearLayout> 
    </LinearLayout> 

    <RelativeLayout 
     android:id="@+id/adspace" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" > 

     <View 
      android:id="@+id/view" 
      android:layout_width="fill_parent" 
      android:layout_height="7dp" 
      android:background="@drawable/view" /> 

     <WebView 
      android:id="@+id/bannerView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_below="@+id/view" /> 
    </RelativeLayout> 

</RelativeLayout> 

這裏是我的代碼:

public class MainClass extends Activity implements OnTouchListener { 

     // =============================================================================== 
     Button sound1, sound2, sound3, sound4, sound5, sound6, sound7, sound8, 
       sound9, sound10, sound11, sound12, sound13, sound14, sound15; 

     // ============================================================================= 
     MediaPlayer mp1, mp2, mp3, mp4, mp5, mp6, mp7, mp8, mp9, mp10, mp11, mp12, 
       mp13, mp14, mp15; 
     Boolean looping = false; 
     // ============================================================= 



     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main_layout); 
      initMpFiles(); 
      setVolumeControlStream(AudioManager.STREAM_MUSIC); 





      sound1 = (Button) findViewById(R.id.sound1); 
      sound2 = (Button) findViewById(R.id.sound2); 
      sound3 = (Button) findViewById(R.id.sound3); 
      sound4 = (Button) findViewById(R.id.sound4); 
      sound5 = (Button) findViewById(R.id.sound5); 
      sound6 = (Button) findViewById(R.id.sound6); 
      sound7 = (Button) findViewById(R.id.sound7); 
      sound8 = (Button) findViewById(R.id.sound8); 
      sound9 = (Button) findViewById(R.id.sound9); 
      sound10 = (Button) findViewById(R.id.sound10); 
      sound11 = (Button) findViewById(R.id.sound11); 
      sound12 = (Button) findViewById(R.id.sound12); 
      sound13 = (Button) findViewById(R.id.sound13); 
      sound14 = (Button) findViewById(R.id.sound14); 
      sound15 = (Button) findViewById(R.id.sound15); 

      sound1.setOnTouchListener(this); 
      sound2.setOnTouchListener(this); 
      sound3.setOnTouchListener(this); 
      sound4.setOnTouchListener(this); 
      sound5.setOnTouchListener(this); 
      sound6.setOnTouchListener(this); 
      sound7.setOnTouchListener(this); 
      sound8.setOnTouchListener(this); 
      sound9.setOnTouchListener(this); 
      sound10.setOnTouchListener(this); 
      sound11.setOnTouchListener(this); 
      sound12.setOnTouchListener(this); 
      sound13.setOnTouchListener(this); 
      sound14.setOnTouchListener(this); 
      sound15.setOnTouchListener(this); 

     } 

     private void initMpFiles() { 
      mp1 = MediaPlayer.create(MainClass.this, R.raw.item1); 
      mp2 = MediaPlayer.create(MainClass.this, R.raw.item2); 
      mp3 = MediaPlayer.create(MainClass.this, R.raw.item3); 
      mp4 = MediaPlayer.create(MainClass.this, R.raw.item4); 
      mp5 = MediaPlayer.create(MainClass.this, R.raw.item5); 
      mp6 = MediaPlayer.create(MainClass.this, R.raw.item6); 
      mp7 = MediaPlayer.create(MainClass.this, R.raw.item7); 
      mp8 = MediaPlayer.create(MainClass.this, R.raw.item8); 
      mp9 = MediaPlayer.create(MainClass.this, R.raw.item9); 
      mp10 = MediaPlayer.create(MainClass.this, R.raw.item10); 
      mp11 = MediaPlayer.create(MainClass.this, R.raw.item11); 
      mp12 = MediaPlayer.create(MainClass.this, R.raw.item12); 
      mp13 = MediaPlayer.create(MainClass.this, R.raw.item13); 
      mp14 = MediaPlayer.create(MainClass.this, R.raw.item14); 
      mp15 = MediaPlayer.create(MainClass.this, R.raw.item15); 
     } 


    } 


     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      int id = v.getId(); 
      switch (id) { 
      case R.id.sound1: 
       if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) { 
        mp1.start(); 
        if (looping == true) { 
         mp1.setLooping(true); 
        }else{ 
         mp1.setLooping(false); 
        } 
        v.setPressed(true); 
        return true; 
       } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) { 
        mp1.pause(); 
        mp1.seekTo(0); 
        v.setPressed(false); 
        clickCounter(); 
        return false; 
       } 
       break; 
      case R.id.sound2: 
       if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) { 
        mp2.start(); 
        if (looping == true) { 
         mp2.setLooping(true); 
        }else{ 
         mp2.setLooping(false); 
        } 
        v.setPressed(true); 
        return true; 
       } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) { 
        mp2.pause(); 
        mp2.seekTo(0); 
        v.setPressed(false); 
        clickCounter(); 
        return false; 
       } 
       break; 
      case R.id.sound3: 
       if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) { 
        mp3.start(); 
        if (looping == true) { 
         mp3.setLooping(true); 
        }else{ 
         mp3.setLooping(false); 
        } 
        v.setPressed(true); 
        return true; 
       } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) { 
        mp3.pause(); 
        mp3.seekTo(0); 
        v.setPressed(false); 
        clickCounter(); 
        return false; 
       } 
       break; 
      case R.id.sound4: 
       if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) { 
        mp4.start(); 
        if (looping == true) { 
         mp4.setLooping(true); 
        }else{ 
         mp4.setLooping(false); 
        } 
        v.setPressed(true); 
        return true; 
       } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) { 
        mp4.pause(); 
        mp4.seekTo(0); 
        v.setPressed(false); 
        clickCounter(); 
        return false; 
       } 
       break; 
      case R.id.sound5: 
       if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) { 
        mp5.start(); 
        if (looping == true) { 
         mp5.setLooping(true); 
        }else{ 
         mp5.setLooping(false); 
        } 
        v.setPressed(true); 
        return true; 
       } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) { 
        mp5.pause(); 
        mp5.seekTo(0); 
        v.setPressed(false); 
        clickCounter(); 
        return false; 
       } 
       break; 
      case R.id.sound6: 
       if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) { 
        mp6.start(); 
        if (looping == true) { 
         mp6.setLooping(true); 
        }else{ 
         mp6.setLooping(false); 
        } 
        v.setPressed(true); 
        return true; 
       } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) { 
        mp6.pause(); 
        mp6.seekTo(0); 
        v.setPressed(false); 
        clickCounter(); 
        return false; 
       } 
       break; 
      case R.id.sound7: 
       if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) { 
        mp7.start(); 
        if (looping == true) { 
         mp7.setLooping(true); 
        }else{ 
         mp7.setLooping(false); 
        } 
        v.setPressed(true); 
        return true; 
       } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) { 
        mp7.pause(); 
        mp7.seekTo(0); 
        v.setPressed(false); 
        clickCounter(); 
        return false; 
       } 
       break; 
      case R.id.sound8: 
       if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) { 
        mp8.start(); 
        if (looping == true) { 
         mp8.setLooping(true); 
        }else{ 
         mp8.setLooping(false); 
        } 
        v.setPressed(true); 
        return true; 
       } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) { 
        mp8.pause(); 
        mp8.seekTo(0); 
        v.setPressed(false); 
        clickCounter(); 
        return false; 
       } 
       break; 
      case R.id.sound9: 
       if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) { 
        mp9.start(); 
        if (looping == true) { 
         mp9.setLooping(true); 
        }else{ 
         mp9.setLooping(false); 
        } 
        v.setPressed(true); 
        return true; 
       } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) { 
        mp9.pause(); 
        mp9.seekTo(0); 
        v.setPressed(false); 
        clickCounter(); 
        return false; 
       } 
       break; 
      case R.id.sound10: 
       if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) { 
        mp10.start(); 
        if (looping == true) { 
         mp10.setLooping(true); 
        }else{ 
         mp10.setLooping(false); 
        } 
        v.setPressed(true); 
        return true; 
       } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) { 
        mp10.pause(); 
        mp10.seekTo(0); 
        v.setPressed(false); 
        clickCounter(); 
        return false; 
       } 
       break; 
      case R.id.sound11: 
       if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) { 
        mp11.start(); 
        if (looping == true) { 
         mp11.setLooping(true); 
        }else{ 
         mp11.setLooping(false); 
        } 
        v.setPressed(true); 
        return true; 
       } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) { 
        mp11.pause(); 
        mp11.seekTo(0); 
        v.setPressed(false); 
        clickCounter(); 
        return false; 
       } 
       break; 
      case R.id.sound12: 
       if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) { 
        mp12.start(); 
        if (looping == true) { 
         mp12.setLooping(true); 
        }else{ 
         mp12.setLooping(false); 
        } 
        v.setPressed(true); 
        return true; 
       } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) { 
        mp12.pause(); 
        mp12.seekTo(0); 
        v.setPressed(false); 
        clickCounter(); 
        return false; 
       } 
       break; 
      case R.id.sound13: 
       if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) { 
        mp13.start(); 
        if (looping == true) { 
         mp13.setLooping(true); 
        }else{ 
         mp13.setLooping(false); 
        } 
        v.setPressed(true); 
        return true; 
       } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) { 
        mp13.pause(); 
        mp13.seekTo(0); 
        v.setPressed(false); 
        clickCounter(); 
        return false; 
       } 
       break; 
      case R.id.sound14: 
       if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) { 
        mp14.start(); 
        if (looping == true) { 
         mp14.setLooping(true); 
        }else{ 
         mp14.setLooping(false); 
        } 
        v.setPressed(true); 
        return true; 
       } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) { 
        mp14.pause(); 
        mp14.seekTo(0); 
        v.setPressed(false); 
        clickCounter(); 
        return false; 
       } 
       break; 
      case R.id.sound15: 
       if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) { 
        mp15.start(); 
        if (looping == true) { 
         mp15.setLooping(true); 
        }else{ 
         mp15.setLooping(false); 
        } 
        v.setPressed(true); 
        return true; 
       } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) { 
        mp15.pause(); 
        mp15.seekTo(0); 
        v.setPressed(false); 
        clickCounter(); 
        return false; 
       } 
       break; 
      } 
      return true; 
     } 
    } 

回答

0

你需要監聽MotionEvent.ACTION_CANCEL和/或MotionEvent.ACTION_OUTSIDE檢測當手指離開你的按鈕。

+0

但是,如何激活其他按鈕? – 2014-09-29 15:43:20

+0

對不起,這是一個好點!你不會在下一個按鈕上獲得ACTION_DOWN,因爲你實際上沒有點擊它,但是你會得到ACTION_MOVE事件。你可以用這些觸發與DOWN事件相同的動作嗎? – TofferJ 2014-09-29 16:05:28

+0

嗯,我試着'ACTION_MOVE'之前它沒有工作。也許我做錯了什麼,但我做了一些研究,似乎它不能用按鈕來完成。要等一下,看看有人知道這個解決方案。不管怎麼說,還是要謝謝你 – 2014-09-30 07:32:52