如何在按下按钮时更改按钮图像,并在释放按钮后恢复正常状态?

时间:2018-11-13 16:02:27

标签: java android

我的XML:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout"
android:layout_gravity="center"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">



<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/image1" />

我的Java:

public class MainActivity extends Activity implements OnTouchListener {

private MediaPlayer mediaPlayer;
LinearLayout linear;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    linear = (LinearLayout) findViewById(R.id.layout);

    final Button zero = (Button) this.findViewById(R.id.button);
    zero.setOnTouchListener(this);

    mediaPlayer = MediaPlayer.create(this, R.raw.hino);

}

@Override
public boolean onTouch(View v, MotionEvent event)
{

    switch (event.getAction())
    {

        case MotionEvent.ACTION_DOWN:
        {
            mediaPlayer.setLooping(true);
            mediaPlayer.start();
            Toast.makeText(MainActivity.this, "clik", Toast.LENGTH_LONG).show();
            linear.setBackgroundColor(Color.RED);

        }

        break;
        case MotionEvent.ACTION_UP:
        {
            mediaPlayer.pause();
            linear.setBackgroundColor(Color.WHITE);
        }
        break;
    }

    return true;
}

}

我希望按钮图像随着歌曲的开头而改变,并返回到歌曲结尾之前的样子。

我已经尝试使用select不起作用,我已经尝试了其他几种方法,但不能,请帮助我。

2 个答案:

答案 0 :(得分:0)

您应该至少在按钮上调用函数onTouch

答案 1 :(得分:0)

首先,您可以先将xml中的Button更改为ImageButton:

<ImageButton
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/image1" />

在您的MainActivity类中,致力于更改imageButton的图像而不是线性布局:

public class MainActivity extends Activity implements OnTouchListener {

private MediaPlayer mediaPlayer;
LinearLayout linear;
ImageButton zero;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    linear = (LinearLayout) findViewById(R.id.layout);

    zero = (ImageButton) this.findViewById(R.id.button);
    zero.setOnTouchListener(this);

    mediaPlayer = MediaPlayer.create(this, R.raw.hino);
}

@Override
public boolean onTouch(View v, MotionEvent event)
{

    switch (event.getAction())
    {

        case MotionEvent.ACTION_DOWN:
        {
            mediaPlayer.setLooping(true);
            mediaPlayer.start();
            Toast.makeText(MainActivity.this, "clik", Toast.LENGTH_LONG).show();
            // If you want to change the image, and not the background color, use setImageResource
            zero.setImageResource(R.drawable.music_has_started_image)
        }

        break;
        case MotionEvent.ACTION_UP:
        {
            mediaPlayer.pause();
            zero.setImageResource(R.drawable.music_has_ended_image)
        }
        break;
    }

    return true;
}

}