在Android中,是否可能有一个Switch
可以为拇指的选中和未选中状态设置不同的图像?
例如,当Switch
处于未检查状态时,我希望拇指使用image_1.png
,而当Switch
处于检查状态时,我希望拇指使用image_2.png
。
我使用以下guide将Switch
的拇指设置为图像,但是该图像适用于选中状态和未选中状态,我想为这两种图像分别创建一个图像状态。
谢谢!
答案 0 :(得分:0)
在this post中,您可以执行以下操作:
yourSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// Set to image_2.png
yourSwitch.setThumbResource(image2); // <-- Using an image from drawable
} else {
// Set to image_1.png
yourSwitch.setThumbResource(image1);
}
}
});
并且如果您需要将Switch
转换为程序形式:
Switch yourSwitch = (Switch) findViewById(R.id.yourSwitch);