Android点击按钮后如何更改图标

5

我想知道是否有一种方法可以在单击按钮时更改按钮图像。

最初,该按钮具有暂停图标。单击后,我希望播放图标被显示。每次单击按钮时,图标应在播放和暂停之间变化。

是否有任何方法能够实现这一点?

XML布局文件:

<ImageButton
    android:id="@+id/play"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="109dp"
    android:src="@drawable/play_btn"
    android:background="@null" />

play_btn.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/pause" android:state_selected="true" />
    <item android:drawable="@drawable/play" />
</selector>
5个回答

15

Android的切换按钮与自定义选择器似乎是您想要的。

您可以使用类似于以下内容的东西来作为按钮选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/play_icon" android:state_checked="true" />
    <item android:drawable="@drawable/pause_icon" android:state_checked="false" />
</selector>

1
这是您唯一需要的吗?因此,如果单击按钮,它将切换到其他可绘制对象,如果再次单击,则会切换回您的第一个可绘制对象? - Azurespot

8
Button mPlayButton;

boolean isPlay = false;
@Override
public void onCreate(){
    mPlayButton = (Button) findViewById(R.id.play);
    // Default button, if need set it in xml via background="@drawable/default"
    mPlayButton.setBackgroundResource(R.drawable.default);
    mPlayButton.setOnClickListener(mTogglePlayButton);
}

View.OnClickListener mTogglePlayButton = new View.OnClickListener(){

    @Override
    public void onClick(View v){
        // change your button background

        if(isPlay){ 
            v.setBackgroundResource(R.drawable.default);
        }else{
            v.setBackgroundResource(R.drawable.playing);
        }

        isPlay = !isPlay; // reverse
    }

};

这个功能很好,但只有当我点击按钮时才会切换。然而,在此之前根本没有按钮(只有 XML 中的“按钮外壳”)。你知道如何让一个按钮出现(使用第一个想要使用的图标),然后让它自动切换吗?我在这方面遇到了困难。我尝试添加一个可见的按钮,但是当我点击它时,切换不起作用,它仅保留在原始设置按钮上。 - Azurespot
1
安装自己的监听器是过度设计,改变背景资源会破坏按钮的其他属性(如按下颜色和形状)。 - Anti Earth

7

在布局中为按钮的xml添加一个onClick字段(需要API级别4及以上)

<ImageButton
    android:id="@+id/play"
    ...
    android:onClick="buttonPressed" />

您的图标是可绘制对象暂停播放

跟踪您的按钮所具有的图标。

private boolean paused = true;

public void buttonPressed(View view) {

    ImageButton button = (ImageButton) view;
    int icon;

    if (paused) {
        paused = false;
        icon = R.drawable.pause;
    else {
        paused = true;
        icon = R.drawable.play;
    }

    button.setImageDrawable(
        ContextCompat.getDrawable(getApplicationContext(), icon));


}

4
这个答案比被接受的答案好,因为它让你只能更改ImageButton的图标而不改变其下面的背景形状(如果你正在使用它)。 - Deemoe

1

0
您可以使用MaterialButton替代Button,然后只需使用setIcon方法设置您的按钮即可:
import com.google.android.material.button.MaterialButton;

MaterialButton mPlayButton;

boolean isPlay = false;
@Override
public void onCreate(){
    mPlayButton = (MaterialButton) findViewById(R.id.play);
}

View.OnClickListener mTogglePlayButton = new View.OnClickListener(){

    @Override
    public void onClick(View v){
        // change your button background

        if(isPlay){ 
            v.setIcon(ContextCompat.getDrawable(this, R.drawable.your_drawable_resource));
        }else{
            v.setIcon(ContextCompat.getDrawable(this, R.drawable.your_drawable_resource2));
        }

        isPlay = !isPlay; // reverse
    }

};

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接