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

时间:2014-09-29 14:54:59

标签: android button border multi-touch ontouchlistener

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

但后来我在屏幕规格较差的设备上测试了我的应用程序(没有multiTouch的设备),我注意到时间只能按下1个按钮,直到手指抬起。

所以我需要这样的东西:

当手指在button1边框内时,将按下按钮1。当手指移动并离开button1边框时,将不再按下按钮1,当手指进入按钮2或按钮4边框或任何其他按钮时,将按下该按钮。所以我需要激活手指所在的按钮。 但是使用我的代码,即使手指离开按钮1边框后按钮1仍然按下。 这是图片: enter image description here

这是我的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>

这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

您需要聆听MotionEvent.ACTION_CANCEL和/或MotionEvent.ACTION_OUTSIDE以检测手指离开按钮的时间。