Blackberry - 复选框组的自定义逻辑

时间:2010-03-12 11:02:27

标签: user-interface blackberry checkbox custom-controls

如果我点击任何复选框,则必须选中所有以前的复选框  “我的逻辑运作” 如果我取消选中一个复选框,则必须取消选中后面的所有复选框  “怎么做” MyLogic适用于风暴,但不适用于其他型号 该怎么做

//well what i want to do is i have  5 checkboxes
class myscreen
{
 chk_service = new CheckboxField[5];                      
 chk_service[0]= new CheckboxField("1",true)
 chk_service[1]= new CheckboxField("2",false)
 chk_service[2]= new CheckboxField("3",false)
 chk_service[3]= new CheckboxField("4",false)
 chk_service[4]= new CheckboxField("5",false)

 CheckboxFieldChangeListener obj = 
    new CheckboxFieldChangeListener(chk_service);
    chk_service[0].setChangeListener(obj);
    chk_service[1].setChangeListener(obj);
    chk_service[2].setChangeListener(obj);
    chk_service[3].setChangeListener(obj);
    chk_service[4].setChangeListener(obj);


  hm4 = new HorizontalFieldManager();
  hm4.add(chk_service[0]);
  hm4.add(chk_service[1]);
  hm4.add(chk_service[2]);
  hm4.add(chk_service[3]);
  hm4.add(chk_service[4]);

add(hm4);
}


    public CheckboxFieldChangeListener (CheckboxField[] arrFields)
    {
        m_arrFields = arrFields;

    }

    public void fieldChanged(Field field, int context)
    {
        if(true == ((CheckboxField) field).getChecked())
        {
            for(int i = 0; i < m_arrFields.length; i++)
            {
                if(m_arrFields[i]==field)
                {
                    //a[j]=i;
                    j++;
                    break;
                }
                else
                {
                CheckboxField oField = m_arrFields[i];
                oField.setChecked(true);
                }
            }           
        }
        a[k] = j;
        if(false == ((CheckboxField) field).getChecked())
        {
            for(int i =field.getIndex(); i < m_arrFields.length; i++)
            {
                if(m_arrFields[i]==field)
                {
                    //a[j]=i;
                    j++;
                    break;
                }
                else
                {
                CheckboxField oField = m_arrFields[i];
                oField.setChecked(false);
                }
            }           
        }


    }  

}

1 个答案:

答案 0 :(得分:0)

试试这段代码,参加巡演,风暴,大胆

alt text http://img78.imageshack.us/img78/70/cbbold.jpg

class MyScreen extends MainScreen {
    int mCheckBoxesCount = 5;
    Vector mCheckBoxes = new Vector();
    CheckBoxListener mCBListener = new CheckBoxListener();    
    public MyScreen() {
        for (int i = 0; i < mCheckBoxesCount; i++) {
            CheckboxField cb = new CheckboxField(String.valueOf(i), false);
            cb.setChangeListener(mCBListener);
            add(cb);
            mCheckBoxes.addElement(cb);
        }
    }    
    class CheckBoxListener implements FieldChangeListener {
        boolean mProgrammatic = false;
        public void fieldChanged(Field field, int context) {
            if (!mProgrammatic) {
                mProgrammatic = true;
                CheckboxField cbField = (CheckboxField) field;
                int index = mCheckBoxes.indexOf(cbField);
                if (cbField.getChecked()) {
                    for (int i = 0; i < index; i++)
                        ((CheckboxField) mCheckBoxes.elementAt(i))
                                .setChecked(true);
                } else {
                    for (int i = index; i < mCheckBoxesCount; i++)
                        ((CheckboxField) mCheckBoxes.elementAt(i))
                                .setChecked(false);
                }
                mProgrammatic = false;
            }
        }
    }
}