我正在尝试创建一个音乐应用程序。它是在Android Studio中开发的。我想让这个按钮在我按下的时候开始发声,在我松开按钮的时候停止,但是我不能让它工作。
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer minus5 = MediaPlayer.create(this, R.raw.min5);
final Button play_min5 = (Button) this.findViewById(R.id.min5);
play_min5.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// PRESSED
minus5.start();
return true; // if you want to handle the touch event
case MotionEvent.ACTION_UP:
// RELEASED
minus5.stop();
return true; // if you want to handle the touch event
}
return false;
}
});
}}
发布于 2017-05-09 21:50:54
试试这个吧。只需写return true而不是false,它就像一个护身符。
play_min5.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
// Your code here
return true;
}
});https://stackoverflow.com/questions/43869502
复制相似问题