当手指移动时,On Touch Listener未释放按钮(Android)

7
我有一个包含15个按钮的RelativeLayout。我正在开发一个新项目,使用OnTouchListener,我希望我的应用程序能够做到以下事情:当用户触摸例如按钮1时,Mp1将开始播放,直到用户松开手指或将其移动到按钮2上,然后按钮2上的mp2应该开始播放,以此类推。
但是问题在于,如果用户移动手指(而不是松开),如果按钮1被按下,它仍将保持按下状态(动作向下),直到用户松开手指。就像这样:
我的问题是:
当手指离开按钮边界时,我需要添加什么来停止按钮并切换到按下哪个按钮(手指所在的位置)?
我的代码:
sound1.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    pressed1 = true;
                    mp1 = MediaPlayer.create(MainClass.this, R.raw.item1);
                    mp1.start();
                    sound1.setBackgroundResource(R.drawable.pad_pressed);
                    if (looping == true) {
                        mp1.setLooping(true);
                    } else if (looping == false) {
                        mp1.setLooping(false);
                    }

                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                    pressed1 = false;
                    mp1.stop();
                    mp1.reset();
                    mp1.release();
                    sound1.setBackgroundResource(R.drawable.pad_normal);
                }
                return true;
            }
        });

您可以使用View.getLocationOnScreen()来检查事件是否在视图内,但它无法帮助您触发另一个按钮的点击,但至少可以阻止对先前按钮的点击。 - ex0ns
你能发布包含按钮的 XML 吗? - Rod_Algonquin
3个回答

1

试试这个:

sound1.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    pressed1 = true;
                    mp1 = MediaPlayer.create(MainClass.this, R.raw.item1);
                    mp1.start();
                    sound1.setBackgroundResource(R.drawable.pad_pressed);
                    if (looping == true) {
                        mp1.setLooping(true);
                    } else if (looping == false) {
                        mp1.setLooping(false);
                    }

                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP || event.getAction() == android.view.MotionEvent.ACTION_CANCEL) {
                    pressed1 = false;
                    mp1.stop();
                    mp1.reset();
                    mp1.release();
                    sound1.setBackgroundResource(R.drawable.pad_normal);
                }
                return true;
            }
        });

ACTION_CANCEL 发生在父级掌握了运动时。你可以在这里找到更多关于MotionEvents的信息。


1

你需要专注于RelativeLayout。

MyActivity.java

public class MyActivity extends Activity {

private RelativeLayout mainLayout;
private TextView myTag;
private TextView xcordview;
private TextView ycordview;

private MediaPlayer mp1;
private String currentTag;
private boolean areaDetected;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    mainLayout = (RelativeLayout) findViewById(R.id.main_layout);
    myTag = (TextView) findViewById(R.id.tag);
    xcordview = (TextView) findViewById(R.id.x_view);
    ycordview = (TextView) findViewById(R.id.y_view);
    currentTag = "";
    initEvent();
}

private void initEvent() {
    mainLayout.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            int x = (int) motionEvent.getX();
            int y = (int) motionEvent.getY();

            xcordview.setText("X : " + String.valueOf(x));
            ycordview.setText("Y : " + String.valueOf(y));

            areaDetected = false;
            for (int i = 0; i < mainLayout.getChildCount(); i++) {
                View currentButton = mainLayout.getChildAt(i);
                if (currentButton instanceof Button) {
                    Button b = (Button) currentButton;
                    String tag = b.getTag().toString();

                    if (!pointInside(x, y, b.getLeft(), b.getRight(), b.getTop(), b.getBottom())) {
                        b.setText("");
                        // b.setBackgroundResource(getResources().getColor(android.R.color.black));
                    } else {
                        areaDetected = true;
                        if (!currentTag.equals(tag)) {
                            currentTag = tag;
                            stopPlaying();
                            b.setText(tag);
                            mp1 = getMediaPlayer(tag);
                            mp1.start();
                            // b.setBackgroundResource(getResources().getColor(android.R.color.white));
                        }
                    }
                }
            }
            if (!areaDetected) {
                currentTag = "";
                stopPlaying();
            }
            myTag.setText("Current tag : " + currentTag);
            return true;
        }
    });
}

private void stopPlaying() {
    if (mp1 != null) {
        mp1.stop();
        mp1.release();
        mp1 = null;
    }
}

private MediaPlayer getMediaPlayer(String tag) {
    if (tag.equals("b1")) {
        return MediaPlayer.create(MyActivity.this, R.raw.coffee_and_snow);
    }
    return MediaPlayer.create(MyActivity.this, R.raw.coffee_and_snow2);
}

static boolean pointInside(int x, int y, int x1, int x2, int y1, int y2) {
    return (x <= x2 && x >= x1 && y <= y2 && y >= y1);
}
}

和 xml 文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MyActivity">

    <com.example.antoine.touchtest.MyButton
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:tag="b1" />

    <com.example.antoine.touchtest.MyButton
        android:id="@+id/b2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/b1"
        android:tag="b2" />

    <com.example.antoine.touchtest.MyButton
        android:id="@+id/b3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/b2"
        android:tag="b3" />

    <com.example.antoine.touchtest.MyButton
        android:id="@+id/b4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/b3"
        android:tag="b4" />

    <TextView
        android:id="@+id/x_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/y_view"
        android:text="X : " />

    <TextView
        android:id="@+id/tag"
        android:layout_above="@+id/x_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Current tag : " />

    <TextView
        android:id="@+id/y_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Y : " />
</RelativeLayout>

最终我的自定义按钮 MyButton
public class MyButton extends Button {

    public MyButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public MyButton(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        // return super.onTouchEvent(event);
        return false;
    }

}

非常感谢这篇文章 ==> 获取按钮坐标并检测手指是否在其上 - Android


是的,这个可以工作了,但有一件事。我以前从未在派生类中使用过OnTouchEvent,我应该如何使用OnTouchEvent?我在哪里可以设置按钮ID,我尝试了我能想到的一切,但都没有起作用。 - Slim C.
你需要使用 onTouchEvent 吗?这个事件被禁用了,因为它与 RelativeLayout 中的 onTouch 冲突。 - mrroboaat
那我该如何让这些按钮实现功能呢?我应该使用什么? - Slim C.
我认为你可以从按钮中使用标签进行操作。我已经更新了我的答案。如果你按下或点击b1,会播放一首歌曲。通过滑动到另一个按钮,会开始新的一首歌曲。如果你移出该区域,歌曲会停止。 - mrroboaat

0
sound1.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
        pressed1 = true;
        mp1 = MediaPlayer.create(MainClass.this, R.raw.item1);
        mp1.start();
        sound1.setBackgroundResource(R.drawable.pad_pressed);
        if (looping == true) {
            mp1.setLooping(true);
        } else if (looping == false) {
            mp1.setLooping(false);
        }
        return true;

    } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
        pressed1 = false;
        mp1.stop();
        mp1.reset();
        mp1.release();
        sound1.setBackgroundResource(R.drawable.pad_normal);
        return true;
    }
    return false;
}});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接