拖放pressing按钮 - 机器人 [英] Dragging and pressing buttons - android

查看:160
本文介绍了拖放pressing按钮 - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个按钮,我想preSS其中之一,并拖动通过另一个presing他们。你能告诉我哪 MotionEvent 或者我应该使用其他功能。我使用 onTouchListener

有一个形象,你可以看到我想要做的(第一个 ACTION_DOWN 1日的按钮,将通过第二次,第7次按键依然pressing屏幕)最后preSS每一个白色的按钮:

下面是我的onTouch按钮code:

 按钮1 =(按钮)findViewById(R.id.button1);

        button1.setOnTouchListener(新OnTouchListener(){

            @覆盖
            公共布尔onTouch(视图V,MotionEvent事件){

                    开关(event.getAction())
                    {
                        案例MotionEvent.ACTION_DOWN:
                            soundIDs [0] = SOUND.PLAY(R.raw.sample1);
                            button1.setBackgroundResource(R.drawable.white_clicked);
                            打破;
                        案例MotionEvent.ACTION_UP:
                        案例MotionEvent.ACTION_CANCEL:
                            sound.stop(soundIDs [0]);
                            button1.setBackgroundResource(R.drawable.white);
                            打破;
                    }
            返回false;
            }
        });
 

解决方案

正在设置OnTouchListener上只是一个按钮。这不会帮助你知道当指针移动(例如,用户拖动手指)到另一个按钮。

您可以对包含按钮的视图OnTouchListener。然后检查ACTION_DOWN,ACTION_MOVE和ACTION_UP事件。那么你将不得不做一些简单的点击检测找出要激活的按钮。

线沿线的东西:

  getWindow()。getDecorView()
    .findViewById(android.R.id.content)
    .setOnTouchListener(新OnTouchListener(){

        @覆盖
        公共布尔onTouch(查看为arg0,MotionEvent事件){
            INT行动= event.getAction();
            如果(动作!= MotionEvent.ACTION_DOWN
                &功放;&安培;行动!= MotionEvent.ACTION_MOVE
                &功放;&安培; !行动= MotionEvent.ACTION_UP)返回false;

            矩形hitRect =新的矩形();
            Button按钮;
            的for(int i = 0; I< myButtons.size();我++){
                钮= myButtons.get(ⅰ);
                button.getHitRect(hitRect);
                如果(hitRect.contains((int)的event.getX(),(int)的event.getY())){
                    button.setText(打!);
                }
            }
            返回true;
        }

    });
 

在哪里myButtons是你的按钮(在你的榜样钢琴键)。

一个ArrayList

另外,你可能要修改这个正确关闭,如果用户的触摸离开按钮,当前活动的按钮,但不打另一个按钮。

我有一个布局在一排3个按钮测试上述code在Android设备上。拖动你的手指在所有的按钮使每个按钮的文字更改为打!

就像我上面说的,你就只有一个按钮设置触摸监听器,这是行不通的。在这个例子中,我设置了触摸监听器在该活动的整个视图。

I have several buttons and I would like to press on one of them and drag through another presing them. Could you tell me which MotionEvent or another functionality should I use. I'm using onTouchListener.

There is an image where you can see what I want to do (first ACTION_DOWN on 1st button and drag through 2nd-7th buttons still pressing the screen) and finally press every white buttons:

Below is my onTouch button code:

button1 = (Button) findViewById(R.id.button1);

        button1.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                    switch (event.getAction())
                    {
                        case MotionEvent.ACTION_DOWN:
                            soundIDs[0] = sound.play(R.raw.sample1);
                            button1.setBackgroundResource(R.drawable.white_clicked);
                            break;
                        case MotionEvent.ACTION_UP:
                        case MotionEvent.ACTION_CANCEL:
                            sound.stop(soundIDs[0]);
                            button1.setBackgroundResource(R.drawable.white);
                            break;
                    }
            return false;
            }
        });

解决方案

You are setting the OnTouchListener on just the one button. That's not going to help you know when the pointer moves (e.g. user drags his finger) into another button.

You could set an OnTouchListener on the view that contains the buttons. Then check for ACTION_DOWN, ACTION_MOVE, and ACTION_UP events. You would then have to do some simple hit detection to figure out which button to activate.

Something along the lines of:

getWindow().getDecorView()
    .findViewById(android.R.id.content)
    .setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent event) {
            int action = event.getAction();
            if (action != MotionEvent.ACTION_DOWN 
                && action != MotionEvent.ACTION_MOVE 
                && action != MotionEvent.ACTION_UP) return false;

            Rect hitRect = new Rect();
            Button button;
            for(int i = 0; i < myButtons.size(); i++) {
                button = myButtons.get(i);
                button.getHitRect(hitRect);
                if (hitRect.contains((int)event.getX(), (int)event.getY())) {
                    button.setText("hit!");
                }
            }
            return true;
        }

    });

Where myButtons is an ArrayList of your buttons (piano keys in your example).

Also, you'd probably want to modify this to properly deactivate the currently active button if the user's touch leaves the button, but doesn't hit another button.

I tested the above code on an android device with a layout that has 3 buttons in a row. Dragging your finger across all the buttons causes each button's text to change to "hit!"

Like I said above, you were setting the touch listener on the just one button, that will not work. In this example I have set the touch listener on the entire view for the activity.

这篇关于拖放pressing按钮 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆