java - 按钮(图像)在被点击后不改变
问题描述
我有一个非常基本的应用程序,带有一个按钮,可以在点击时播放声音。它会一直播放,直到按下/点击按钮,一旦您取消点击或取消触摸它,声音就会停止。您点击(按住)声音会继续播放,一旦按钮未被触摸,声音就会停止。现在我有一个按钮图像,只要它被触摸(音乐播放)就会改变,并且在未触摸(音乐停止)时返回默认状态。当我使用 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>
解决方案
触摸侦听器消耗点击侦听器,它永远不会到达选择器。由于您正在使用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
成为局部变量
推荐阅读
- vue.js - 如何在 vue 组件的 sass-loader 中使用“sass:meta”
- c++ - 如何告诉 Visual Studio 的静态分析器某个函数验证了先决条件?
- php - 在进行资源丰富的 CRUD 时,整页 livewire 组件与 laravel 控制器
- flutter - 未处理的异常:类型“WebViewController”不是类型转换中“String”类型的子类型
- django - LANGUAGE_SESSION_KEY 不是英文时,Django 表单无效
- javascript - Axios Error 401 (Unauthorized) 尝试获取 api 时出错
- javascript - 如何使用仅包装我的 JavaScript 代码的 npm 包?
- azure-object-anchors - Azure 对象锚点 - Azure 对象锚点支持哪些 3D 模型格式?
- octave - I want to stop the code when x is equal to 1
- shell - shell脚本读取一行并逐行处理