【问题标题】:On Touch Listener does not release button when finger is moved Android当手指移动Android时,On Touch Listener不会释放按钮
【发布时间】:2014-07-25 12:56:04
【问题描述】:

我有一个带有 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() 检查事件是否在视图内,它不会帮助您触发单击另一个按钮,但它至少可以停止单击前一个按钮一个
  • 能不能把按钮所在的xml贴出来

标签: android button move ontouchlistener


【解决方案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 发生在父级控制该动议时。你可以找到更多关于MotionEventshere.

【讨论】:

    【解决方案2】:

    你必须专注于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>
    

    最后是我的自定义按钮

    我的按钮

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

    非常感谢这篇文章 ==> Get button coordinates and detect if finger is over them - Android

    【讨论】:

    • 是的,这行得通,但有一件事。我以前从未在派生类中使用过 OnTouchEvent,我应该如何使用 onTouchEvent?我在哪里可以设置按钮 ID,我尝试了所有我能想到的方法。
    • 您需要onTouchEvent 吗?这里这个事件被禁用了,因为它和RelativeLayout的onTouch冲突。
    • 那么我还能如何设置这些按钮来做某事?我应该使用什么?
    • 我认为您可以使用 Button 中的标签。我已经更新了我的答案。如果您按或单击 b1,您就有歌曲。通过滑动到另一个按钮开始一首新歌。如果您移出该区域,歌曲就会停止。
    【解决方案3】:
    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;
    }});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-29
      • 2011-04-22
      • 2014-03-15
      • 2018-09-20
      • 1970-01-01
      • 2013-10-27
      相关资源
      最近更新 更多