简体   繁体   English

单击时更改android按钮图标

[英]android change button icon when clicked

I want to know if there is a way of changing a button image when it is clicked. 我想知道在点击按钮图像时是否有更改按钮图像的方法。 Initially the button has a pause icon. 最初,按钮有一个暂停图标。 When it is clicked I want the play icon to be displayed. 单击它时,我希望显示播放图标。 Each time the button is clicked the icon should vary between play and pause. 每次单击该按钮时,图标应在播放和暂停之间变化。

Is there any way of doing this? 有没有办法做到这一点?

XML Layout file: 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 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>

Android's Toggle Button with a custom selector sounds like what you want. 带有自定义选择器的Android's Toggle Button听起来就像你想要的那样。

You might use something like this for your button's selector: 你可以使用这样的东西作为按钮的选择器:

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

Let's add an onClick field to your button's xml in your layout (requires API level 4 and onwards) 让我们在你的布局中为你的按钮的xml添加一个onClick字段(需要API级别4及以后)

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

Your icons are drawables pause and play . 您的图标是可绘制的pauseplay

Keep track of which icon your button has. 跟踪按钮的图标。

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


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

};

Checkout this documentation http://developer.android.com/reference/android/widget/ImageButton.html specifically the inherited methods from android.widget.imageview. 查看此文档http://developer.android.com/reference/android/widget/ImageButton.html特别是android.widget.imageview中继承的方法。 You'll want to use either setImageBitmap or setImageDrawable. 您将要使用setImageBitmap或setImageDrawable。 Update it as part of your on click code. 将其更新为点击代码的一部分。

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

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