简体   繁体   English

单击时更改的按钮

[英]Button to change when clicked

Button onTouchLitsener does not change when clicked. 单击按钮onTouchLitsener不会更改。 I want the button to change when clicked. 我希望按钮在单击时更改。

    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;
    }

}

my xml file 我的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>

I'd recommend to use an onClickListener . 我建议使用onClickListener

The onTouchListener is receiving two events when pressing the Button - ACTION_DOWN when you're touching it and ACTION_UP when releasing. 按下按钮时,onTouchListener会收到两个事件-触摸时为ACTION_DOWN,释放时为ACTION_UP。 So the player starts and stops right after that. 因此,播放器随即开始和停止。

You have written button code in wrong block. 您在错误的块中编写了按钮代码。

you have written zero.setOnTouchListener(this); 您已经写了zero.setOnTouchListener(this); so whenever you will touch button onTouch will get called not onTouchEvent 因此,只要您触摸按钮,onTouch就会被调用,而不是onTouchEvent

So add button code in onTouch. 因此,在onTouch中添加按钮代码。

Make following change in your code. 在您的代码中进行以下更改。

Remove this entire block 删除整个块

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;
    }

Move above code in onTouch block.Below is how it should be. 在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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM