【问题标题】:Press button when finger is inside button view, android OnTouchListener当手指在按钮视图内时按下按钮,android OnTouchListener
【发布时间】:2014-09-29 14:54:59
【问题描述】:

我正在开发具有 15 个按钮的应用程序。每个按钮都有不同的声音。我希望在按下按钮时播放声音,当抬起手指停止声音并且效果很好时,LogCat 中不会显示延迟和错误/警告。

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

所以我需要这样的东西:

当手指在按钮 1 的边界内时,按钮 1 将被按下。当手指移动并离开 button1 边框时,按钮 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>

这是我的代码:

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

【问题讨论】:

    标签: android button border multi-touch ontouchlistener


    【解决方案1】:

    您需要监听MotionEvent.ACTION_CANCEL 和/或MotionEvent.ACTION_OUTSIDE 来检测手指何时离开您的按钮。

    【讨论】:

    • 但是那要怎么激活其他按钮呢?
    • 对不起,这是一个好点!由于您实际上并未单击它,因此您不会在下一个按钮上获得 ACTION_DOWN,但您将获得 ACTION_MOVE 事件。您可以使用它们来触发与 DOWN 事件相同的操作吗?
    • 好吧,我试过ACTION_MOVE,但它没有用。也许我做错了什么,但我做了一些研究,似乎不能用按钮来完成。等着看有没有人知道解决办法。还是谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    相关资源
    最近更新 更多