[英]Android - Custom multi-click Checkbox with Images
I want to create a custom Checkbox with four different states: 我想创建具有四个不同状态的自定义复选框:
I did found here how to create a checkbox with custom images (with the states pressed, focused, hovered and default). 我确实在这里找到了如何创建一个带有自定义图像的复选框(按下状态,焦点状态,悬停状态和默认状态)。 But what I want instead is a way to cycle between the four states as follows: 但是我想要的是一种在四个状态之间循环的方法,如下所示:
PS: I know it's probably easier in an android app to just make a dropdown with the 4 states or a pop-up where you select one of the states. PS:我知道在android应用程序中仅用4种状态进行下拉或在其中选择一种状态的弹出窗口中可能会更容易。 But I want among these two, this third option above, so users can decide for themselves which of the three settings they'll prefer. 但是我想在上面的第三个选项中选择这两个,以便用户可以自己决定他们更喜欢这三个设置中的哪个。
PSS: While I'm typing this I came up with the idea of a button with the unchecked image, and when you click it it will replace the image-src to the next, while keeping everything else the same (like width/height and margin/padding). PSS:在键入此内容时,我想到了一个带有未选中图像的按钮的想法,当您单击它时,它将把image-src替换为下一个,同时保持其他所有内容不变(例如宽度/高度和边距/填充)。 Is this the best approach for this situation, or does someone have a more elegant solution instead? 这是针对这种情况的最佳方法,还是有人有更优雅的解决方案?
Thanks in advance for the responses. 预先感谢您的答复。
As stated in the PSS, I only had the idea for the solution when I was typing the question above. 正如PSS中所述,我只有在输入上述问题时才想到解决方案。 I've used an ImageButton that changes it's source when it's clicked. 我使用了一个ImageButton,它在单击时会更改其来源。 Here is the code: 这是代码:
ImageButton in xml: xml中的ImageButton:
<ImageButton
android:id="@+id/ibtnCheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/checkbox_content_description"
android:src="@drawable/checkbox_unchecked"
android:background="@drawable/transparent_button_background" />
transparent_button_background.xml: transparent_button_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@android:color/transparent" />
<item android:state_pressed="true" android:drawable="@android:color/transparent" />
<item android:drawable="@android:color/transparent" />
</selector>
Activity: 活动:
public class MainActivity extends ActionBarActivity {
private ImageButton cbButton;
private int status;
private int checkbox_images[] = {
R.drawable.checkbox_unchecked,
R.drawable.checkbox_checked,
R.drawable.checkbox_error,
R.drawable.checkbox_partly
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = 0;
addListenerToButton();
}
private void addListenerToButton(){
cbButton = (ImageButton) findViewById(R.id.ibtnCheckbox);
cbButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
switch(status){
// Unchecked -> Green checked
case 0:
// Green checked -> Red cross
case 1:
// Red cross -> Orange-yellow checked
case 2:
cbButton.setImageResource(checkbox_images[++status]);
break;
// Orange-yellow checked -> Unchecked
case 3:
cbButton.setImageResource(checkbox_images[status = 0]);
break;
// Default (just in case)
default:
status = 0;
cbButton.setImageResource(checkbox_images[status++]);
break;
}
}
});
}
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.