Android:点击时更改按钮颜色

时间:2015-04-20 20:46:32

标签: java android

基本上,我正在尝试创建一个单击按钮(注意: 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>

2 个答案:

答案 0 :(得分:8)

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

这就是每个州按钮的最终外观 enter image description here

答案 1 :(得分:0)

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

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

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

v.setSelected( !v.isSelected() );
相关问题