Android 如何在每次单击时更改按钮的图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10470288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:51:20  来源:igfitidea点击:

how to change the image of a button with every click?

androidbuttonimagebuttonxml-drawable

提问by Androelpha

I created a buttonin the layout. In the Drawablefolder I created a XML filenamed btn01_state. The btn01_state.xmlis assigned to the buttoni created through "android:background=@drawable/btn01_state"

buttonlayout. 在Drawable文件夹中,我创建了一个XML file名为btn01_state. 将btn01_state.xml被分配给button通过创建我“ android:background=@drawable/btn01_state

Now, the buttonhas a default imageimg1.when i clickon the button, the image1 changes to img2, and once i release the clicked mouse button, the image2 again changed to img1 again.

现在,button有一个默认的imageimg1.when iclick在 上button,image1 更改为 img2,一旦我释放单击的鼠标按钮,image2 再次更改为 img1。

what i want to do is,to change the image of the button with evey click.

我想要做的是,每次点击更改按钮的图像。

for an example, initially btn01 has img01

例如,最初 btn01 有 img01

if btn01 is pressed==> set img of btn01 to img02 and keep img02 till the btn01 is pressed again. Now, btn01 has img02 on it.

如果按下 btn01==> 将 btn01 的 img 设置为 img02 并保持 img02 直到再次按下 btn01。现在,btn01 上有 img02。

When btn01 is pressed, set img01 to btn01.

当按下 btn01 时,将 img01 设置为 btn01。

I hope this clarified more what i want to do.

我希望这能更清楚地说明我想要做什么。

btn_selector:

btn_selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/android_blue"
      android:state_pressed="true" />
<item android:drawable="@drawable/ic_launcher"
      android:state_focused="true" />
<item android:drawable="@drawable/ic_launcher" />

main.xml

主文件

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

回答by Murat Nafiz

You can do it easily within the code.

您可以在代码中轻松完成。

boolean isPressed = false;
button.setOnClickListener(buttonListener);

OnClickListener buttonListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        if(isPressed)
           button.setBackgroundResource(R.drawable.icon1);
        else
           button.setBackgroundResource(R.drawable.icon2);

        isPressed = !isPressed;
   }
};

回答by Parag Chauhan

Simple way

简单的方法

btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
    btn.setBackgroundDrawable(getResources().getDrawable(R.drawable.locationbutton_on));
                }
        }); 

回答by Warpzit

Make it in code perhaps. Put a listener on the button and when the button is clicked the background is changed.

也许在代码中制作它。在按钮上放置一个侦听器,单击按钮时背景会发生变化。