2012-06-17 2 views
0

클릭하면 onTouchLitener 버튼이 변경되지 않습니다. 클릭하면 버튼이 바뀌길 원합니다.클릭하면 변경되는 버튼

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에서 버튼 코드를 추가하지 불려가는 만질 때마다.

코드에서 다음과 같이 변경하십시오.

제거이 전체 블록 onTouch block.Below 코드 위

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; 
    } 

이동은 어떻게해야입니다.

@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에 다른 코드를 유지합니까? 똑같다? 또한 당신의 anwser에 감사드립니다 – elcuban

+0

만약 mp.setLooping (true); mp.start(); 그렇다면 해당 사항 없습니다. –

0

은 내가 onClickListener을 사용하는 것이 좋습니다 것입니다.

onTouchListener는 Button - ACTION_DOWN을 터치 할 때 두 이벤트를받습니다. 터치 할 때 ACTION_UP을 놓을 때 두 이벤트가 수신됩니다. 그래서 플레이어는 그 직후에 시작하고 멈 춥니 다.

+0

오, 그렇지만 어떻게하면 음악을 재생하고 동시에 변경할 수 있습니까? 그 두 가지 이벤트가 있어야합니까? sory는 아직 newby – elcuban

+0

입니다. 왜 false로 기본 설정으로 재생이라는 부울을 추가하지 않으십니까? 당신은 if를 사용하고 boolean을 체크 한 다음 나중에 playing =! playing을 설정할 수 있습니다. 재생하지 않는 경우 버튼 텍스트를 중지하고 재생을 시작합니다. 재생을 시작하고 중지하려면 텍스트를 설정하십시오.) – Tim

관련 문제