首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

button按上事件开始播放音乐的有关问题

2012-09-07 
button按下事件开始播放音乐的问题在播放音乐时我们习惯用button,而且也习惯Button SoundButton2 (Butto

button按下事件开始播放音乐的问题

在播放音乐时我们习惯用button,而且也习惯

Button SoundButton2 = (Button)findViewById(R.id.sound2);?
? ? ? ? SoundButton2.setOnClickListener(new OnClickListener() {?
?
? ? public void onClick(View v) {?
? ? ? ? mSoundManager.playSound(2);?
?
? ? }?
});?
这样你呢 你会发现 你按下去并咩有直接播放,直达你松开

那怎么才可以呢

b.setOnTouchListener(new OnTouchListener() {?
?
? ? ? ? ? ? @Override?
? ? ? ? ? ? public boolean onTouch(View v, MotionEvent event) {?
? ? ? ? ? ? ? ? if ( event.equals(MotionEvent.ACTION_DOWN ) ) {?
? ? ? ? ? ? ? ? ? ? mSoundManager.playSound(2);?
? ? ? ? ? ? ? ? ? ? return true;?
? ? ? ? ? ? ? ? }?
?
? ? ? ? ? ? ? ? return false;?
? ? ? ? ? ? }?
? ? ? ? });

这里要注意返回false两个,以便让她接受其他事件

据个例子来说,如果你这个是个Imagebutton你想让他点击的时候既可以换背景 又可以播放音乐

ImageButton SoundButton1 = (ImageButton)findViewById(R.id.sound1); SoundButton1.setImageResource(R.drawable.my_button);

SoundButton1.setOnTouchListener(new OnTouchListener() {?
?
? ? ? ? public boolean onTouch(View v, MotionEvent event) {?
? ? ? ? ? ? if (event.getAction() == MotionEvent.ACTION_DOWN ) {?
? ? ? ? ? ? ? ? mSoundManager.playSound(1);?
? ? ? ? ? ? ? ? return true;?
? ? ? ? ? ? }?
?
? ? ? ? ? ? return false;?
? ? ? ? }?
? ? });?

如果你用上面的代码 你就会发现不能 换图片,只要你吧 return true也会返回成 return false就可以了

热点排行