【问题标题】:Android: Change button color when clickedAndroid:单击时更改按钮颜色
【发布时间】:2015-04-20 20:46:31
【问题描述】:

基本上,我正在尝试创建一个按钮,单击该按钮(注意:NOT 按下)会将颜色从 color1 更改为 color2。再次单击时,它会从 color2 变回 color1。

我已经疯狂地搜索,我设法提取的唯一信息是如何在按下按钮时更改颜色,即当用户按住按钮时(此代码将在下面编写)。但是,我希望当用户单击(按下并释放)按钮时颜色会发生变化,然后在用户再次单击时变回来。

这个文件在 res/drawable 中

<!-- Changes color when user hols down button -->
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true">
    <shape>
        <!-- change to color2 -->
    </shape>
</item>

<item>
    <shape>
        <!-- change to color1 -->
    </shape>
</item>
</selector>

【问题讨论】:

    标签: java android


    【解决方案1】:
    boolean tmp = false;
    button.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
             tmp = !tmp;
             v.setBackgroundColor(tmp ? Color.RED : Color.BLUE);
        }
    });
    

    编辑:显然你想要一个更复杂的例子

    首先在其中创建一个可绘制的 XML 并将其命名为 pink_button.xml 并将以下代码放入其中

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
           android:shape="rectangle">
    
        <solid android:color="#FF5EF1"/>
        <corners android:radius="15dp"/>
        <stroke
            android:width="1dp"
            android:color="#303030"/>
    
    </shape>
    

    现在制作一个blue_button.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
           android:shape="rectangle">
    
        <solid android:color="#008DFF"/>
        <corners android:radius="15dp"/>
        <stroke
            android:width="1dp"
            android:color="#303030"/>
    
    </shape>
    

    现在做一些演示活动布局,我使用了button_demo_activity.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    
        <Button
            android:id="@+id/btnDemo"
            android:layout_width="150dp"
            android:layout_height="30dp"
            android:layout_centerInParent="true"
            android:layout_marginTop="100dp"
            android:background="@drawable/pink_button"
            android:gravity="center"
            android:text="PINK"
            android:textColor="@android:color/white"
            android:textSize="15sp"/>
    
    </RelativeLayout>
    

    最后是活动,我用的任何你想要的名字ButtonDemoActivity

    public class ButtonDemoActivity extends Activity {
    
    
        private Button btnDemo;
        private boolean isPink = true;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.button_demo_activity);
    
            btnDemo = (Button) findViewById(R.id.btnDemo);
            btnDemo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    isPink = !isPink;
                    int resId = isPink ? R.drawable.pink_button : R.drawable.blue_button;
                    btnDemo.setBackgroundResource(resId);
                    btnDemo.setText(isPink ? "PINK" : "BLUE");
                }
            });
        }
    }
    

    这就是按钮在每个状态下的最终外观

    【讨论】:

    • 是的,这种工作,除了按钮完全改变,当我尝试这个。基本上,它没有圆角等。但我猜你也可以在 onClick 方法中更改它?
    • 当然。而不是改变颜色,使用 setbackgroundDrawable(R.drawable.something) ,你可以像你的问题一样有一个可绘制的自定义形状。我的回答是为了简化事情:)
    • 首先,感谢您的简化,非常感谢!好吧,我想我明白了,但由于某种原因无法让它工作。你能以某种方式表明你的意思吗?
    • 我为您添加了一个简单但有用的示例。干杯
    【解决方案2】:

    你在正确的轨道上。添加其他状态,例如:

    <item android:state_selected="true">
      <shape>
        <!-- change to color2 -->
      </shape>
    </item>
    

    然后在您的 onClick 方法中添加一些切换:

    v.setSelected( !v.isSelected() );
    

    【讨论】:

    • 我尝试再添加一个状态,但这并没有帮助。现在我没有onClick 方法。我的按钮的activity.xml 看起来像这样(...) android:background="@drawable/custom_button"/&gt; 其中custom_button 是来自主帖子的xml 文件。那么您对我应该如何编写onClick 方法有什么建议吗?除了你写的那一行。
    • 你可以尝试使用ToggleButton,它不需要onClick并且可以保持它的状态。尽管您必须为此提供 selector 中的状态