【问题标题】:how to change button background color as clicked action?如何将按钮背景颜色更改为单击操作?
【发布时间】:2013-07-18 02:42:04
【问题描述】:

---在安卓应用中---

单击按钮后,其背景颜色变为所需的。再次点击,颜色会恢复原色。

如何实现?

任何回复,谢谢!

================================================ ==================================== 更新:从网络上,我找到了实现这一目标的方法。 像这样在 xml 中设计一个可绘制的颜色:

<drawable name="button_checked">#ffff0000</drawable>  

在activity中,使用以下代码获取Drawable对象:

Resources resource = getBaseContext().getResources();
checked_drawable = resource.getDrawable(R.drawable.button_checked);

在onClick函数中,根据一个布尔变量:

 setBackgroundDrawable(checked_mDrawable)

设置按钮背景。

【问题讨论】:

标签: android button click


【解决方案1】:

在你的js文件里放

$('#button').toggleClass('bg');

在您的 css 中,使用常规按钮 css 样式

#button { background: white; }

然后添加

#button.bg { background: red; }

所以当他们点击按钮时背景会变成红色,如果他们再次点击它会变回白色。

使用您想要的任何颜色/网址作为背景。

【讨论】:

  • 感谢您的回复!我在android应用程序中使用java,那么如何实现呢?
【解决方案2】:

你可以在代码中动态地做(我不知道如何在xml中配置它)。

boolean flag=true;
Button button=(Button)findViewById(R.id.button);
oncreate()
{
    button.setBackgroundResource(R.drawable.first_time_click);
    button.setOnClickListener(new OnClickListener()
    {
    public void onClick(View v)
    {
        if (flag)
        {
            button.setBackgroundResource(R.drawable.odd_time_click);        
        }else
        {
           button.setBackgroundResource(R.drawable.even_time_click);
        }
        flag=!flag;
    }
    });
}

【讨论】:

    【解决方案3】:

    你只需要像下面这样创建a state list drawable resource

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:drawable="@drawable/btn_login_pressed" android:state_pressed="true"/>
        <item android:drawable="@drawable/btn_login_normal" android:state_pressed="false"/>
    
    </selector>
    

    更新: 也许你想要 RadioButton 的效果,这里是一个例子:

        <RadioButton
           android:id="@+id/tab_communication"
           android:layout_width="wrap_content"
           android:layout_height="40dp"
           android:layout_weight="1"
           android:background="@null"
           android:button="@null"
           android:drawableTop="@drawable/category_communication"
           android:paddingBottom="5dp"
           android:paddingTop="5dp" />
    

    drawable/category_communication.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:drawable="@drawable/category_communication_checked" android:state_checked="true"/>
        <item android:drawable="@drawable/category_communication_normal" android:state_checked="false"/>
    
     </selector>
    

    ToggleButton 是另一种选择。

    【讨论】:

    • 我刚刚测试了你的方法。但它显示的不是我希望的。我想要的是当按钮单击并向上时,它的背景颜色会更改为保持颜色 A。当我再次单击它时,它的背景颜色会更改以恢复原始颜色。
    • 我想知道如何在通用按钮中实现此功能,而不是 RadioButton。谢谢!
    • 对于通用按钮,由于它没有可检查的属性,我不知道实现。愿你能关注弃儿的回答。
    • [ToggleButton]developer.android.com/reference/android/widget/… 是另一种选择
    • 感谢您的建议!使用 ToggleButton 也是个好主意!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-09
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 1970-01-01
    相关资源
    最近更新 更多