当手指在按钮视图内时按下按钮,android OnTouchListener

当手指在按钮视图内时按下按钮,android OnTouchListener,android,button,border,multi-touch,ontouchlistener,Android,Button,Border,Multi Touch,Ontouchlistener,我正在开发的应用程序将有15个按钮。每个按钮都有不同的声音。我想在按下按钮播放声音时,当抬起手指停止声音时,它工作正常,LogCat中不会显示延迟和错误/警告 但后来我在屏幕规格不好的设备(没有多点触控的设备)上测试了我的应用程序,我注意到每次只能按下一个按钮,直到手指抬起为止 所以我需要这样的东西: 当手指位于按钮1边框内时,按钮1将被按下。当手指移动并离开按钮1边框时,将不再按下按钮1,当手指进入按钮2或按钮4边框或任何其他按钮时,将按下该按钮。所以我需要激活手指所在的按钮。 但根据我的代码

我正在开发的应用程序将有15个按钮。每个按钮都有不同的声音。我想在按下按钮播放声音时,当抬起手指停止声音时,它工作正常,LogCat中不会显示延迟和错误/警告

但后来我在屏幕规格不好的设备(没有多点触控的设备)上测试了我的应用程序,我注意到每次只能按下一个按钮,直到手指抬起为止

所以我需要这样的东西:

当手指位于按钮1边框内时,按钮1将被按下。当手指移动并离开按钮1边框时,将不再按下按钮1,当手指进入按钮2或按钮4边框或任何其他按钮时,将按下该按钮。所以我需要激活手指所在的按钮。 但根据我的代码,即使手指离开按钮1的边框,按钮1仍然被按下。 以下是图片:

以下是我的XML布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="@drawable/pad_background_selector" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/view" >

        <ImageButton
            android:id="@+id/record_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:background="@null"
            android:src="@drawable/static_selector_record" />

        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true" />

        <ImageButton
            android:id="@+id/info_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:background="@null"
            android:src="@drawable/static_selector_info" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/adspace"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/relativeLayout1"
        android:baselineAligned="false"
        android:orientation="horizontal"
        android:weightSum="3" >

        <LinearLayout
            android:id="@+id/first"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:weightSum="5" >

            <Button
                android:id="@+id/sound1"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:layout_margin="4.8dp"
                android:layout_weight="1"
                android:background="@drawable/static_pad_selector"
                tools:ignore="NestedWeights" />

            <Button
                android:id="@+id/sound4"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:layout_margin="4.8dp"
                android:layout_weight="1"
                android:background="@drawable/static_pad_selector" />

            <Button
                android:id="@+id/sound7"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:layout_margin="4.8dp"
                android:layout_weight="1"
                android:background="@drawable/static_pad_selector" />

            <Button
                android:id="@+id/sound10"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:layout_margin="4.8dp"
                android:layout_weight="1"
                android:background="@drawable/static_pad_selector" />

            <Button
                android:id="@+id/sound13"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:layout_margin="4.8dp"
                android:layout_weight="1"
                android:background="@drawable/static_pad_selector" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/third"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:weightSum="5" >

            <Button
                android:id="@+id/sound2"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:layout_margin="4.8dp"
                android:layout_weight="1"
                android:background="@drawable/static_pad_selector"
                tools:ignore="NestedWeights" />

            <Button
                android:id="@+id/sound5"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:layout_margin="4.8dp"
                android:layout_weight="1"
                android:background="@drawable/static_pad_selector" />

            <Button
                android:id="@+id/sound8"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:layout_margin="4.8dp"
                android:layout_weight="1"
                android:background="@drawable/static_pad_selector" />

            <Button
                android:id="@+id/sound11"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:layout_margin="4.8dp"
                android:layout_weight="1"
                android:background="@drawable/static_pad_selector" />

            <Button
                android:id="@+id/sound14"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:layout_margin="4.8dp"
                android:layout_weight="1"
                android:background="@drawable/static_pad_selector" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/second"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:weightSum="5" >

            <Button
                android:id="@+id/sound3"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:layout_margin="4.8dp"
                android:layout_weight="1"
                android:background="@drawable/static_pad_selector"
                tools:ignore="NestedWeights" />

            <Button
                android:id="@+id/sound6"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:layout_margin="4.8dp"
                android:layout_weight="1"
                android:background="@drawable/static_pad_selector" />

            <Button
                android:id="@+id/sound9"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:layout_margin="4.8dp"
                android:layout_weight="1"
                android:background="@drawable/static_pad_selector" />

            <Button
                android:id="@+id/sound12"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:layout_margin="4.8dp"
                android:layout_weight="1"
                android:background="@drawable/static_pad_selector" />

            <Button
                android:id="@+id/sound15"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:layout_margin="4.8dp"
                android:layout_weight="1"
                android:background="@drawable/static_pad_selector" />
        </LinearLayout>
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/adspace"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" >

        <View
            android:id="@+id/view"
            android:layout_width="fill_parent"
            android:layout_height="7dp"
            android:background="@drawable/view" />

        <WebView
            android:id="@+id/bannerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/view" />
    </RelativeLayout>

</RelativeLayout>

您需要监听
MotionEvent.ACTION\u CANCEL
和/或
MotionEvent.ACTION\u OUTSIDE
以检测手指何时离开按钮。

但这将如何激活其他按钮?抱歉,这是一个好观点!您不会在“下一步”按钮上看到动作,因为您没有实际单击它,但会看到动作移动事件。你能用它们来触发与停机事件相同的动作吗?我尝试了
动作\u MOVE
,但没有成功。也许我做错了什么,但我做了一些研究,似乎按钮是做不到的。我等着看有没有人知道解决办法。无论如何谢谢你
   public class MainClass extends Activity implements OnTouchListener {

        // ===============================================================================
        Button sound1, sound2, sound3, sound4, sound5, sound6, sound7, sound8,
                sound9, sound10, sound11, sound12, sound13, sound14, sound15;

        // =============================================================================
        MediaPlayer mp1, mp2, mp3, mp4, mp5, mp6, mp7, mp8, mp9, mp10, mp11, mp12,
                mp13, mp14, mp15;
        Boolean looping = false;
        // =============================================================



        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main_layout);
            initMpFiles();
            setVolumeControlStream(AudioManager.STREAM_MUSIC);





            sound1 = (Button) findViewById(R.id.sound1);
            sound2 = (Button) findViewById(R.id.sound2);
            sound3 = (Button) findViewById(R.id.sound3);
            sound4 = (Button) findViewById(R.id.sound4);
            sound5 = (Button) findViewById(R.id.sound5);
            sound6 = (Button) findViewById(R.id.sound6);
            sound7 = (Button) findViewById(R.id.sound7);
            sound8 = (Button) findViewById(R.id.sound8);
            sound9 = (Button) findViewById(R.id.sound9);
            sound10 = (Button) findViewById(R.id.sound10);
            sound11 = (Button) findViewById(R.id.sound11);
            sound12 = (Button) findViewById(R.id.sound12);
            sound13 = (Button) findViewById(R.id.sound13);
            sound14 = (Button) findViewById(R.id.sound14);
            sound15 = (Button) findViewById(R.id.sound15);

            sound1.setOnTouchListener(this);
            sound2.setOnTouchListener(this);
            sound3.setOnTouchListener(this);
            sound4.setOnTouchListener(this);
            sound5.setOnTouchListener(this);
            sound6.setOnTouchListener(this);
            sound7.setOnTouchListener(this);
            sound8.setOnTouchListener(this);
            sound9.setOnTouchListener(this);
            sound10.setOnTouchListener(this);
            sound11.setOnTouchListener(this);
            sound12.setOnTouchListener(this);
            sound13.setOnTouchListener(this);
            sound14.setOnTouchListener(this);
            sound15.setOnTouchListener(this);   

        }

        private void initMpFiles() {
            mp1 = MediaPlayer.create(MainClass.this, R.raw.item1);
            mp2 = MediaPlayer.create(MainClass.this, R.raw.item2);
            mp3 = MediaPlayer.create(MainClass.this, R.raw.item3);
            mp4 = MediaPlayer.create(MainClass.this, R.raw.item4);
            mp5 = MediaPlayer.create(MainClass.this, R.raw.item5);
            mp6 = MediaPlayer.create(MainClass.this, R.raw.item6);
            mp7 = MediaPlayer.create(MainClass.this, R.raw.item7);
            mp8 = MediaPlayer.create(MainClass.this, R.raw.item8);
            mp9 = MediaPlayer.create(MainClass.this, R.raw.item9);
            mp10 = MediaPlayer.create(MainClass.this, R.raw.item10);
            mp11 = MediaPlayer.create(MainClass.this, R.raw.item11);
            mp12 = MediaPlayer.create(MainClass.this, R.raw.item12);
            mp13 = MediaPlayer.create(MainClass.this, R.raw.item13);
            mp14 = MediaPlayer.create(MainClass.this, R.raw.item14);
            mp15 = MediaPlayer.create(MainClass.this, R.raw.item15);
        }


    }


        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int id = v.getId();
            switch (id) {
            case R.id.sound1:
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    mp1.start();
                    if (looping == true) {
                        mp1.setLooping(true);
                    }else{
                        mp1.setLooping(false);
                    }
                    v.setPressed(true);
                    return true;
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                    mp1.pause();
                    mp1.seekTo(0);
                    v.setPressed(false);
                    clickCounter();
                    return false;
                }
                break;
            case R.id.sound2:
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    mp2.start();
                    if (looping == true) {
                        mp2.setLooping(true);
                    }else{
                        mp2.setLooping(false);
                    }
                    v.setPressed(true);
                    return true;
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                    mp2.pause();
                    mp2.seekTo(0);
                    v.setPressed(false);
                    clickCounter();
                    return false;
                }
                break;
            case R.id.sound3:
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    mp3.start();
                    if (looping == true) {
                        mp3.setLooping(true);
                    }else{
                        mp3.setLooping(false);
                    }
                    v.setPressed(true);
                    return true;
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                    mp3.pause();
                    mp3.seekTo(0);
                    v.setPressed(false);
                    clickCounter();
                    return false;
                }
                break;
            case R.id.sound4:
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    mp4.start();
                    if (looping == true) {
                        mp4.setLooping(true);
                    }else{
                        mp4.setLooping(false);
                    }
                    v.setPressed(true);
                    return true;
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                    mp4.pause();
                    mp4.seekTo(0);
                    v.setPressed(false);
                    clickCounter();
                    return false;
                }
                break;
            case R.id.sound5:
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    mp5.start();
                    if (looping == true) {
                        mp5.setLooping(true);
                    }else{
                        mp5.setLooping(false);
                    }
                    v.setPressed(true);
                    return true;
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                    mp5.pause();
                    mp5.seekTo(0);
                    v.setPressed(false);
                    clickCounter();
                    return false;
                }
                break;
            case R.id.sound6:
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    mp6.start();
                    if (looping == true) {
                        mp6.setLooping(true);
                    }else{
                        mp6.setLooping(false);
                    }
                    v.setPressed(true);
                    return true;
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                    mp6.pause();
                    mp6.seekTo(0);
                    v.setPressed(false);
                    clickCounter();
                    return false;
                }
                break;
            case R.id.sound7:
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    mp7.start();
                    if (looping == true) {
                        mp7.setLooping(true);
                    }else{
                        mp7.setLooping(false);
                    }
                    v.setPressed(true);
                    return true;
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                    mp7.pause();
                    mp7.seekTo(0);
                    v.setPressed(false);
                    clickCounter();
                    return false;
                }
                break;
            case R.id.sound8:
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    mp8.start();
                    if (looping == true) {
                        mp8.setLooping(true);
                    }else{
                        mp8.setLooping(false);
                    }
                    v.setPressed(true);
                    return true;
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                    mp8.pause();
                    mp8.seekTo(0);
                    v.setPressed(false);
                    clickCounter();
                    return false;
                }
                break;
            case R.id.sound9:
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    mp9.start();
                    if (looping == true) {
                        mp9.setLooping(true);
                    }else{
                        mp9.setLooping(false);
                    }
                    v.setPressed(true);
                    return true;
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                    mp9.pause();
                    mp9.seekTo(0);
                    v.setPressed(false);
                    clickCounter();
                    return false;
                }
                break;
            case R.id.sound10:
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    mp10.start();
                    if (looping == true) {
                        mp10.setLooping(true);
                    }else{
                        mp10.setLooping(false);
                    }
                    v.setPressed(true);
                    return true;
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                    mp10.pause();
                    mp10.seekTo(0);
                    v.setPressed(false);
                    clickCounter();
                    return false;
                }
                break;
            case R.id.sound11:
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    mp11.start();
                    if (looping == true) {
                        mp11.setLooping(true);
                    }else{
                        mp11.setLooping(false);
                    }
                    v.setPressed(true);
                    return true;
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                    mp11.pause();
                    mp11.seekTo(0);
                    v.setPressed(false);
                    clickCounter();
                    return false;
                }
                break;
            case R.id.sound12:
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    mp12.start();
                    if (looping == true) {
                        mp12.setLooping(true);
                    }else{
                        mp12.setLooping(false);
                    }
                    v.setPressed(true);
                    return true;
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                    mp12.pause();
                    mp12.seekTo(0);
                    v.setPressed(false);
                    clickCounter();
                    return false;
                }
                break;
            case R.id.sound13:
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    mp13.start();
                    if (looping == true) {
                        mp13.setLooping(true);
                    }else{
                        mp13.setLooping(false);
                    }
                    v.setPressed(true);
                    return true;
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                    mp13.pause();
                    mp13.seekTo(0);
                    v.setPressed(false);
                    clickCounter();
                    return false;
                }
                break;
            case R.id.sound14:
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    mp14.start();
                    if (looping == true) {
                        mp14.setLooping(true);
                    }else{
                        mp14.setLooping(false);
                    }
                    v.setPressed(true);
                    return true;
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                    mp14.pause();
                    mp14.seekTo(0);
                    v.setPressed(false);
                    clickCounter();
                    return false;
                }
                break;
            case R.id.sound15:
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    mp15.start();
                    if (looping == true) {
                        mp15.setLooping(true);
                    }else{
                        mp15.setLooping(false);
                    }
                    v.setPressed(true);
                    return true;
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                    mp15.pause();
                    mp15.seekTo(0);
                    v.setPressed(false);
                    clickCounter();
                    return false;
                }
                break;
            }
            return true;
        }
    }