標籤:
理論基礎
RadioButton是一種特定類型的兩狀態按鈕,它與複選框相似。它可以 和RadioButtonGroup一起使用,形成一個"組"。繼承自AbstractCheckButton,AbstractCheckButton繼承自Widget。
代碼部分
void addEventListener (const ccRadioButtonCallback &callback)
註冊一個回呼函數, 它將在選項按鈕被選中或者取消的時候被調用.
bool isSelected () const
查詢是否選中複選框。
void setSelected (bool selected)
修改複選框的狀態 設定True會使複選框變為“選中狀態”,否則傳False會使複選框變為“未選中狀態”
void loadTextures (const std::string &background,//背景普通狀態圖片的名字。
const std::string &backgroundSelected,//背景選擇狀態圖片的名字。
const std::string &cross,//勾選選中狀態圖片的名字。
const std::string &backgroundDisabled,//背景禁用狀態圖片名字。
const std::string &frontCrossDisabled,//勾選禁用狀態圖片名字。
TextureResType texType=TextureResType::LOCAL)//
載入所有紋理並初始化一個複選框。
void loadTextureBackGround (const std::string &backGround, TextureResType type=TextureResType::LOCAL)
載入背景普通狀態紋理
void loadTextureBackGroundSelected (const std::string &backGroundSelected, TextureResType texType=TextureResType::LOCAL)
載入背景選中狀態紋理的圖片
void loadTextureFrontCross (const std::string &crossTextureName, TextureResType texType=TextureResType::LOCAL)
載入勾選選中狀態的紋理圖片
void loadTextureBackGroundDisabled (const std::string &backGroundDisabled, TextureResType texType=TextureResType::LOCAL)
載入背景禁用狀態紋理的圖片
void loadTextureFrontCrossDisabled (const std::string &frontCrossDisabled, TextureResType texType=TextureResType::LOCAL)
載入勾選禁用狀態的紋理圖片
virtual Size getVirtualRendererSize () const override
擷取虛擬渲染節點的尺寸
virtual Node * getVirtualRenderer () override
控制項的內部渲染節點。 例如,一個按鈕的虛擬渲染節點是它的紋理節點。
void setZoomScale (float scale)
當使用者按下複選框時,按鈕將會縮放到一個比例 最後複選框的縮放值等於(複選框原尺寸+ _zoomScale)
float getZoomScale () const
返回一個縮放比例
Sprite * getRendererBackground () const
返回背景普通狀態的渲染節點
Sprite * getRendererBackgroundSelected () const
返回背景選擇狀態的渲染節點
Sprite * getRendererFrontCross () const
返回前景普通狀態的渲染節點
Sprite * getRendererBackgroundDisabled () const
返回背景禁用狀態的渲染節點
Sprite * getRendererFrontCrossDisabled () const
返回前景禁用狀態的渲染節點
執行個體:
Size widgetSize = _widget->getContentSize(); // Create a radio button group _radioButtonGroup = RadioButtonGroup::create(); _uiLayer->addChild(_radioButtonGroup); // Create the radio buttons static const int NUMBER_OF_BUTTONS = 5; startPosX = widgetSize.width / 2.0f - ((NUMBER_OF_BUTTONS - 1) / 2.0f) * BUTTON_WIDTH; for(int i = 0; i < NUMBER_OF_BUTTONS; ++i) { RadioButton* radioButton = RadioButton::create("cocosui/radio_button_off.png", "cocosui/radio_button_on.png"); float posX = startPosX + BUTTON_WIDTH * i; radioButton->setPosition(Vec2(posX, widgetSize.height / 2.0f + 10)); radioButton->setScale(1.2f); _radioButtonGroup->addRadioButton(radioButton); _uiLayer->addChild(radioButton); }
cocos代碼研究(16)Widget子類RadioButton學習筆記