2013-07-13 5 views
-1

나는 파이프 응용 프로그램을 만들고 있습니다. 내 파이프에는 6 개의 구멍이있어서 6 개의 버튼으로 간주합니다. 아시다시피, 한 번에 하나, 두 개 또는 그 이상의 구멍 (여기서 버튼)을 잡으면 차이가납니다. 나는 안드로이드에서 새로운데 어떻게 관리 할 수 ​​있습니까? 예를 들어 나는이 작업을 수행 할 수 : 단추 1 및 단추 2가 동시에 접촉 할 경우,
: button6와 단추 2가 동시에 접촉 할 경우, Sound1 (사운드 1)
재생 : sound2
재생 단추 1 및 단추 2 및 buuton3이 동시에 접촉 할 경우 : sound3를 연주하십시오.
.
.
.android에서 한 번에 두 개 이상의 버튼을 터치

one.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 

      switch(event.getAction()){ 

      case MotionEvent.ACTION_DOWN: 

       is1t = true; 
       soundtoplay(); 
       one.setBackgroundResource(R.drawable.hintedholes); 
       return true; 

      case MotionEvent.ACTION_UP: 

       is1t = false; 
       soundtoplay(); 
       one.setBackgroundResource(R.drawable.holes); 
       return true; 

      } 
      return false; 
     } 
    }); 

    two.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 

      switch(event.getAction()){ 

      case MotionEvent.ACTION_DOWN: 

       is2t = true; 
       soundtoplay(); 
       two.setBackgroundResource(R.drawable.hintedholes); 
       return true; 

      case MotionEvent.ACTION_UP: 

       is2t = false; 
       soundtoplay(); 
       two.setBackgroundResource(R.drawable.holes); 
       return true; 

      } 
      return false; 
     } 
    }); 

    three.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 

      switch(event.getAction()){ 

      case MotionEvent.ACTION_DOWN: 

       is3t = true; 
       soundtoplay(); 
       three.setBackgroundResource(R.drawable.hintedholes); 
       return true; 

      case MotionEvent.ACTION_UP: 

       is3t = false; 
       soundtoplay(); 
       three.setBackgroundResource(R.drawable.holes); 
       return true; 

      } 
      return false; 
     } 
    }); 
... 

public void soundtoplay(){ 

    if(is1t == true && is2t == false && is3t == false && is4t == false && is5t == false && is6t == false){ 


     mp = MediaPlayer.create(Playing.this, R.raw.b); 
     mp.start(); 

    }else if(is1t == true && is2t == true && is3t == false && is4t == false && is5t == false && is6t == false){ 

     mp = MediaPlayer.create(Playing.this, R.raw.a); 
     mp.start(); 

    }else if(is1t == true && is2t == true && is3t == true && is4t == false && is5t == false && is6t == false){ 

     mp = MediaPlayer.create(Playing.this, R.raw.g1); 
     mp.start(); 

    }else if ... 

답변

-1

가 울부 짖는 코드를 사용 해보세요 :
마지막으로 나는이 트릭을 사용했다.

공용 클래스 MainActivity는 활동 OnTouchListener가

{

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    findViewById(R.id.button1).setOnTouchListener(this); 
    findViewById(R.id.button2).setOnTouchListener(this); 
    findViewById(R.id.button3).setOnTouchListener(this); 
    findViewById(R.id.button4).setOnTouchListener(this); 
    findViewById(R.id.button5).setOnTouchListener(this); 
    findViewById(R.id.button6).setOnTouchListener(this); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

public void onToch(View v) { 
    // TODO Auto-generated method stub 
    switch (v.getId()) { 
    case R.id.button1: 
     Log.d("TAG", "button1 click"); 
     break; 
    case R.id.button2: 
     Log.d("TAG", "button2 click"); 
     break; 
    case R.id.button3: 
     Log.d("TAG", "button3 click"); 
     break; 
    case R.id.button4: 
     Log.d("TAG", "button4 click"); 
     break; 
    case R.id.button5: 
     Log.d("TAG", "button5 click"); 
     break; 
    case R.id.button6: 
     Log.d("TAG", "button6 click"); 
     break; 

    default: 
     break; 
    } 
} 

} 안드로이드에서

0

4.1 (내 생각), 당신은 아무것도하지 않고 여러 단일 터치을 트리거 할 수 있습니다 구현 확장합니다. 그러나 더 낮은 버전을 지원하려면 일반적인 멀티 터치 처리 방법을 사용하고 다른 조건을 처리 할 수 ​​있습니다. this을 확인하십시오.

0

한 가지 방법은 onClickListener을 설정하는 대신 onTouchListener을 단추에 설정하는 것입니다. 이런 식으로 버튼을 클릭 할 때마다 작업을 수행 할 필요가 없습니다.
그런 다음 첫 번째 클릭과 마지막 클릭 사이의 시간을 측정하고이 간격 동안 클릭 한 버튼을 조절하여 동시 터치를 처리해야합니다.

+0

우리가 어떻게 만지는 지 상관없이, 우리는 서로에게 지체없이 그들을 만집니다. 클릭 시간을 어떻게 측정 할 수 있습니까? 예를 들어 나는 이렇게 sth 할 필요가있다 : button2가 button1에서 100 밀리 초 후에 건드린다면 나는 그것들을 동시에 만졌을 까? –

+0

클릭 사이의 지연은 구현에 따라 다릅니다. 이 링크를보고 첫 번째 클릭과 마지막 클릭 사이의 시간을 측정하는 방법에 대한 아이디어를 얻을 수 있습니다. http://stackoverflow.com/questions/12762272/android-countdowntimer-additional-milliseconds-delay-between-ticks – SAbbasizadeh

관련 문제