单击时要更改的按钮

时间:2012-06-17 16:06:09

标签: java android ontouchlistener

按钮onTouchListener在单击时不会更改。我想要点击时更改按钮。

    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>

2 个答案:

答案 0 :(得分:0)

我建议使用onClickListener

onTouchListener在按下按钮时会收到两个事件 - 触摸它时为ACTION_DOWN,释放时为ACTION_UP。所以玩家在此之后开始和停止。

答案 1 :(得分:0)

您已在错误的区块中编写了按钮代码。

你写了zero.setOnTouchListener(this);所以每当你触摸按钮onTouch都会被调用而不是onTouchEvent

所以在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;
    }