答案 0 :(得分:1)
您必须定义一个扩展Checkbox的类
定义检查状态的变量
重写onClick或onCheck方法,在3个状态之间更改该变量,而不是切换isChecked变量。
static private final int CHECKED = 1;
private int state;
public CheckBoxTriStates(Context context) {
super(context);
init();
}
public CheckBoxTriStates(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CheckBoxTriStates(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init()
{
state = UNKNOW;
updateBtn();
setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
// checkbox status is changed from uncheck to checked.
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
switch (state)
{
case UNKNOW:
state = UNCHECKED;
break;
case UNCHECKED:
state = CHECKED;
break;
case CHECKED:
state = UNKNOW;
break;
}
updateBtn();
}
});
}
private void updateBtn()
{
int btnDrawable = R.drawable.ic_checkbox_indeterminate_black;
switch (state)
{
case UNKNOW:
btnDrawable = R.drawable.ic_checkbox_indeterminate_black;
break;
case UNCHECKED:
btnDrawable = R.drawable.ic_checkbox_unchecked_black;
break;
case CHECKED:
btnDrawable = R.drawable.ic_checkbox_checked_black;
break;
}
setButtonDrawable(btnDrawable);
}
public int getState()
{
return state;
}
public void setState(int state)
{
this.state = state;
updateBtn();
}
}