由于我是Android编程的新手,我需要一些帮助来实现以下场景:
我有两个按钮(1和2)一个在另一个之上,我需要实现以下三个动作:
以下代码实现1(除了移动内部按钮的情况)和3b)。
boolean pressedUp = false;
SoundPool p1;
SoundPool p2;
int s1;
int s2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
final Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
button1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (pressedUp == false) {
pressedUp = true;
p1.play(s1, 1, 1, 1, 0, 1);
}
case MotionEvent.ACTION_UP:
{
p1.autoPause();
pressedUp = false;
}
case MotionEvent.ACTION_MOVE:
{
p1.autoPause();
pressedUp = false;
}
}
return true;
}
});
button2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (pressedUp == false) {
pressedUp = true;
p2.play(s2, 1, 1, 1, 0, 1);
}
case MotionEvent.ACTION_UP:
{
p2.autoPause();
pressedUp = false;
}
case MotionEvent.ACTION_MOVE:
{
p2.autoPause();
pressedUp = false;
}
}
return true;
}
});
p1 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
s1 = p1.load(this, R.raw.a, 1);
p2 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
s2 = p2.load(this, R.raw.b, 1);
}
我很感激任何帮助来实现整个场景。