2012-06-17 226 views
0

單擊時onTouchLitsener按鈕不會更改。點擊時我想讓按鈕改變。單擊時更改按鈕

public class SoundActivity extends Activity implements OnTouchListener { 
    /** Called when the activity is first created. */ 
    MediaPlayer mp; 
    MediaPlayer mp1; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     setVolumeControlStream(AudioManager.STREAM_MUSIC); 


     final Button zero = (Button) this.findViewById(R.id.button1); 
     zero.setOnTouchListener(this); 

     mp = MediaPlayer.create(this, R.raw.song_3); 

     //final ImageButton zero = (ImageButton) this.findViewById(R.id.imageButton1); 
     //zero.setOnTouchListener(this); 

     //mp = MediaPlayer.create(this, R.raw.song_3); 
    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) 
    { 
     switch (event.getAction()) 
     { 
     case MotionEvent.ACTION_DOWN: 
     { 
      mp.setLooping(true); 
      mp.start(); 
     } 
     break; 
     case MotionEvent.ACTION_UP: 
     { 
      mp.pause(); 
     } 
     break; 
    } 
    return true; 
    } 
    //public boolean onTouchEvent(View v, MotionEvent event) { 
     //ImageView iv = (ImageView) v; 

     // if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      // iv.setImageResource(R.drawable.arrow_leftpressed); 
      // return true; 
     //} else if (event.getAction() == MotionEvent.ACTION_UP) { 
      // iv.setImageResource(R.drawable.arrow_left); 
      //return true; 
     //} 

     //return false; 
    //} 

    public boolean onTouchEvent(View v, MotionEvent event) { 
     Button zero = (Button) v; 

     if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      zero.setBackgroundResource(R.drawable.arrow_leftpressed); 
      return true; 
     } else if (event.getAction() == MotionEvent.ACTION_UP) { 
      zero.setBackgroundResource(R.drawable.arrow_left); 
      return true; 
     } 
     return false; 
    } 

} 

我的XML文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="button" 
     android:clickable="true" 
     /> 

    <ImageButton 
     android:id="@+id/imageButton1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/button4" 
     android:clickable="true" 
     /> 

</LinearLayout> 

回答

0

你寫錯了代碼塊的按鈕代碼。

你寫zero.setOnTouchListener(this);所以每當你會觸動按鈕onTouch將調用不onTouchEvent

所以在onTouch添加按鈕的代碼。

在代碼中進行以下更改。

刪除整個塊

public boolean onTouchEvent(View v, MotionEvent event) { 
     Button zero = (Button) v; 

     if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      zero.setBackgroundResource(R.drawable.arrow_leftpressed); 
      return true; 
     } else if (event.getAction() == MotionEvent.ACTION_UP) { 
      zero.setBackgroundResource(R.drawable.arrow_left); 
      return true; 
     } 

     return false; 
    } 

移動上述onTouch block.Below代碼是應該的。

@Override 
    public boolean onTouch(View v, MotionEvent event) { 
     Button zero = (Button) v; 

     if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      zero.setBackgroundResource(R.drawable.arrow_leftpressed); 
      return true; 
     } else if (event.getAction() == MotionEvent.ACTION_UP) { 
      zero.setBackgroundResource(R.drawable.arrow_left); 
      return true; 
     } 

     return false; 
    } 
+0

即將嘗試它,但我有一個問題,我仍然保持其他代碼在onTouch一樣?如果你正在談論mp.setLooping(true),也感謝你的支持者 – elcuban

+0

; mp.start(); 然後是它不是一個問題 –

0

我建議你使用一個onClickListener

當按下按鈕時,onTouchListener正在接收兩個事件 - 觸摸它時觸發ACTION_DOWN,釋放時觸發ACTION_UP。所以玩家在此之後開始並停止。

+0

哦,但我怎麼能讓它播放音樂和同時改變,dowsnt它必須是兩個事件呢? sory仍然是一個新的 – elcuban

+0

爲什麼不只是添加一個布爾型稱爲默認玩false?你可以使用一個if並檢查布爾值,然後設置播放=!播放。如果它沒有播放設置按鈕文本停止並開始播放,如果它正在播放設置文本啓動和停止它;) – Tim