答案 0 :(得分:1)
ToggleButton Btn=new ToggleButton(this);// or get it from the layout by ToggleButton Btn=(ToggleButton) findViewById(R.id.IDofButton);
Btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
buttonView.setBackgroundColor(Color.GREEN);
else buttonView.setBackgroundColor(Color.RED);
}
});
在可绘制文件夹中,创建一个xml文件my_btn_toggle.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="@color/red" />
<item android:state_checked="true" android:drawable="@color/green" />
</selector>
并在xml部分中定义切换按钮:
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="@+id/toggleButton"
android:background="@drawable/my_btn_toggle"/>
最后但并非最不重要:)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/bg_selected" android:state_checked="true" android:state_pressed="true"/>
<item android:drawable="@drawable/bg_selected" android:state_checked="true" android:state_focused="false"/>
<item android:drawable="@drawable/bg_normal" android:state_checked="false" android:state_pressed="true"/>
<item android:drawable="@drawable/bg_normal" android:state_checked="false" android:state_focused="false"/>
答案 1 :(得分:0)
<ToggleButton
android:background="@drawable/toggle_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
use the below drawable in the above toggle button
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Drop Shadow Stack -->
<item>
<selector>
<item android:state_pressed="true">
<shape>
<corners android:radius="3dp" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<solid android:color="@color/transparent" />
</shape>
</item>
<item>
<shape>
<corners android:radius="3dp" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<solid android:color="#00CCCCCC" />
</shape>
</item>
</selector>
</item>
<item>
<selector>
<item android:state_pressed="true">
<shape>
<corners android:radius="3dp" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<solid android:color="@color/transparent" />
</shape>
</item>
<item>
<shape>
<corners android:radius="3dp" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<solid android:color="#10CCCCCC" />
</shape>
</item>
</selector>
</item>
<item>
<selector>
<item android:state_pressed="true">
<shape>
<corners android:radius="3dp" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<solid android:color="@color/transparent" />
</shape>
</item>
<item>
<shape>
<corners android:radius="3dp" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<solid android:color="#20CCCCCC" />
</shape>
</item>
</selector>
</item>
<item>
<selector>
<item android:state_pressed="true">
<shape>
<corners android:radius="3dp" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<solid android:color="@color/transparent" />
</shape>
</item>
<item>
<shape>
<corners android:radius="3dp" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<solid android:color="#30CCCCCC" />
</shape>
</item>
</selector>
</item>
<item>
<selector>
<item android:state_pressed="true">
<shape>
<corners android:radius="3dp" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<solid android:color="@color/transparent" />
</shape>
</item>
<item>
<shape>
<corners android:radius="3dp" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<solid android:color="#50CCCCCC" />
</shape>
</item>
</selector>
</item>
<item>
<selector>
<item android:state_checked="false">
<shape>
<corners android:radius="3dp" />
<padding android:bottom="5dp" />
<solid android:color="@android:color/black" />
</shape>
</item>
<item android:state_checked="true">
<shape>
<corners android:radius="3dp" />
<padding android:bottom="5dp" />
<solid android:color="@android:color/white" />
</shape>
</item>
</selector>
</item>
<!-- Background -->
<item android:id="@+id/toggle_background">
<shape>
<corners
android:topLeftRadius="3dp"
android:topRightRadius="3dp" />
<padding android:bottom="5dp" />
<solid android:color="@android:color/holo_orange_dark" />
</shape>
</item>
</layer-list>
答案 2 :(得分:0)
如何改变风格?
styles.xml
<style name="ToggleButton" parent="Base.Widget.AppCompat.Button">
<item name="colorAccent">select your color</item>
<style>
现在在布局中的“切换”按钮上设置样式。
<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/ToggleButton"/>
答案 3 :(得分:0)
[更改切换按钮的颜色] [1]
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="@+id/toggleButton"
android:textColor="#f26e08"
android:textSize="30dp"
android:padding="20dp"
android:background="@drawable/selector"
android:layout_gravity="center_horizontal" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="@+id/toggleButton2"
android:textColor="#f26e08"
android:background="@drawable/selector"
android:textSize="30dp"
android:padding="20dp"
android:layout_gravity="center_horizontal" />
selector.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/toggleon"
android:state_checked="true"/>
<item
android:drawable="@drawable/toggleoff"
android:state_checked="false"/>
</selector>
toggleon.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke android:color="#0a8e9b"
android:width="6dp"/>
</shape>
</item>
<item android:bottom="6dp">
<shape android:shape="rectangle">
<solid
android:color="#b5f4f2"/>
</shape>
</item>
</layer-list>
toggleoff.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke android:color="#f26e08"
android:width="6dp"/>
</shape>
</item>
<item android:bottom="6dp">
<shape android:shape="rectangle">
<solid android:color="#56fd58"/>
</shape>
</item>
</layer-list>````
[1]: https://i.stack.imgur.com/R2oOV.png