2012-06-17 231 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