首页 > 解决方案 > 按钮(图像)在被点击后不改变

问题描述

我有一个非常基本的应用程序,带有一个按钮,可以在点击时播放声音。它会一直播放,直到按下/点击按钮,一旦您取消点击或取消触摸它,声音就会停止。您点击(按住)声音会继续播放,一旦按钮未被触摸,声音就会停止。现在我有一个按钮图像,只要它被触摸(音乐播放)就会改变,并且在未触摸(音乐停止)时返回默认状态。当我使用 onClickListener 时,这件事工作正常,但是一旦我将其更改为 onTouchListener ,按钮图像在点击时就不会改变。这是 MainActivity.java 代码:

package com.example.firozkaoo2222.myapplication;

import android.media.MediaPlayer;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import static com.example.firozkaoo2222.myapplication.R.raw.police;

public class MainActivity extends AppCompatActivity {


private MediaPlayer policeSound;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button policeSounds = findViewById(R.id.police);

    policeSounds.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (policeSound == null) {
                policeSound = MediaPlayer.create(getApplicationContext(), R.raw.police);
            }

            int eventPadTouch = event.getAction();

            switch (eventPadTouch) {

                case MotionEvent.ACTION_DOWN:
                    // start playing sound , in your case:
                    policeSound.start();
                    return true;


                case MotionEvent.ACTION_UP:
                    // stop playing sound , in your case:
                    policeSound.pause();
                    return true;

            }
            return false;
        }
    });}}

activity_main.xml 的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center"
android:orientation="vertical">


<Button
    android:id="@+id/police"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:background="@drawable/custom_button" />
</RelativeLayout>

cutom_button.xml 的代码:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_pressed="true"
    android:drawable="@drawable/button_pressed" />

<item
    android:drawable="@drawable/button_default" />
</selector>

标签: javaandroidbutton

解决方案


触摸侦听器消耗点击侦听器,它永远不会到达选择器。由于您正在使用pressed state,因此您可以模仿按下的状态,policeSounds.setPressed(boolean)因此请更改它

switch (eventPadTouch) {

    case MotionEvent.ACTION_DOWN:
        // start playing sound , in your case:
        policeSound.start();
        policeSounds.setPressed(true)
        return true;


    case MotionEvent.ACTION_UP:
        // stop playing sound , in your case:
        policeSound.pause();
        policeSounds.setPressed(false)
        return true;

}

注意:为了说服,您可以policeSounds在外部声明oncreate而不是使其final成为局部变量


推荐阅读