在android中同时触摸多个按钮

时间:2013-07-13 10:14:25

标签: android multi-touch ontouchlistener

我正在做一个横褶的申请。我的横笛有6个洞,我认为它们是6个按钮。如你所知,如果你同时持有一个,两个或更多个孔(这里是按钮),它会有所不同。我是android新手,我该怎么办呢?例如,我想这样做:
如果同时触摸button1和button2:播放声音1 如果同时触摸按钮6和按钮2:播放声音2
如果同时触摸button1和button2和buuton3:播放声音3



最后我使用了这个技巧:

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 ...

3 个答案:

答案 0 :(得分:1)

在android 4.1+(我猜)中,您可以触发多次单次触摸而无需执行任何操作。但是,要支持较低版本,您可以使用普通的多点触控处理方法,并处理不同的条件。检查this

答案 1 :(得分:0)

一种方法是在按钮上设置onTouchListener,而不是设置onClickListener。这样,每次单击按钮时都不必执行操作 然后,您必须通过测量第一次单击和最后一次单击之间的时间以及在此间隔中单击的按钮上的条件来处理同时触摸。

答案 2 :(得分:-1)

请尝试使用以下代码。

公共类MainActivity扩展了Activity实现的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;
    }
}

}