Java 单击时要更改的按钮
单击时,触摸屏上的按钮不会更改。我希望在单击时更改按钮Java 单击时要更改的按钮,java,android,ontouchlistener,Java,Android,Ontouchlistener,单击时,触摸屏上的按钮不会更改。我希望在单击时更改按钮 public class SoundActivity extends Activity implements OnTouchListener { /** Called when the activity is first created. */ MediaPlayer mp; MediaPlayer mp1; @Override public void onCreate(Bundle save
public class SoundActivity extends Activity implements OnTouchListener {
/** Called when the activity is first created. */
MediaPlayer mp;
MediaPlayer mp1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
final Button zero = (Button) this.findViewById(R.id.button1);
zero.setOnTouchListener(this);
mp = MediaPlayer.create(this, R.raw.song_3);
//final ImageButton zero = (ImageButton) this.findViewById(R.id.imageButton1);
//zero.setOnTouchListener(this);
//mp = MediaPlayer.create(this, R.raw.song_3);
}
@Override
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
{
mp.setLooping(true);
mp.start();
}
break;
case MotionEvent.ACTION_UP:
{
mp.pause();
}
break;
}
return true;
}
//public boolean onTouchEvent(View v, MotionEvent event) {
//ImageView iv = (ImageView) v;
// if (event.getAction() == MotionEvent.ACTION_DOWN) {
// iv.setImageResource(R.drawable.arrow_leftpressed);
// return true;
//} else if (event.getAction() == MotionEvent.ACTION_UP) {
// iv.setImageResource(R.drawable.arrow_left);
//return true;
//}
//return false;
//}
public boolean onTouchEvent(View v, MotionEvent event) {
Button zero = (Button) v;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
zero.setBackgroundResource(R.drawable.arrow_leftpressed);
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
zero.setBackgroundResource(R.drawable.arrow_left);
return true;
}
return false;
}
}
我的xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"
android:clickable="true"
/>
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/button4"
android:clickable="true"
/>
</LinearLayout>
我建议使用
当按下按钮时,onTouchListener接收到两个事件-触摸按钮时按下ACTION_,释放按钮时按下ACTION_。因此,播放机会立即启动和停止。您在错误的块中编写了按钮代码 您已经编写了
zero.setOnTouchListener(这个)代码>因此,每当您触摸触摸按钮时,都会调用notonTouchEvent
所以在onTouch中添加按钮代码
在代码中进行以下更改
移除整个块
public boolean onTouchEvent(View v, MotionEvent event) {
Button zero = (Button) v;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
zero.setBackgroundResource(R.drawable.arrow_leftpressed);
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
zero.setBackgroundResource(R.drawable.arrow_left);
return true;
}
return false;
}
将onTouch块中的代码移到上面。下面是它应该是什么样子
@Override
public boolean onTouch(View v, MotionEvent event) {
Button zero = (Button) v;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
zero.setBackgroundResource(R.drawable.arrow_leftpressed);
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
zero.setBackgroundResource(R.drawable.arrow_left);
return true;
}
return false;
}
哦,但是我怎样才能让它同时播放音乐和改变,难道不需要两件事吗?对不起,我还是个新手,为什么不添加一个名为playing的布尔值作为默认值?您可以使用if并检查布尔值,然后设置playing=!玩。如果未播放,则将按钮文本设置为停止并开始播放;如果正在播放,则将文本设置为开始并停止;)我现在就要试一下,但我有一个问题,我是否仍然保持onTouch中的其他代码不变?另外,如果您谈论的是mp.setLooping(true),也感谢您的回答;mp.start();那么是的,这不是一个问题