Categories: CrossApp 教程

CAAlertView(提示框)

类说明

CAAlertView是提示框控件,如果提示框内的按钮个数不超过三个,这个横向排列按钮,如果按钮个数超过三个则纵向排列。


CAAlertView 方法 (点击方法名可查看方法介绍)

方法 说明
addButton 添加一个按钮到CAAlertView
setAlertMessage 提示框的提示信息
hide 隐藏提示框
setMessageFontName 提示信息的字体
show 显示提示框
setTarget 添加监听
setTitle 提示框的标题
hideWithDisplayed 隐藏提示框
create 创建
createWithText 创建,并指定其Text
initWithText 初始化,并指定化文本
addButton 添加按钮到CAAlertView

CAAlertView是提示框控件,如果提示框内的按钮个数不超过三个,这个横向排列按钮,如果按钮个数超过三个则纵向排列。


我们来看一下实例代码:
首先在.h文件中声明一下函数:

//按钮的回调函数
    void respondTouch(CAControl* btn, DPoint point);
 
//提示框的回调函数
    void alertViewCallback(int btnIndex);

然后在.cpp文件中添加以下代码:

void FirstViewController::viewDidLoad()
{
    //获取屏幕宽度
    DSize size = this->getView()->getBounds().size;
     
    //设置背景颜色为黑色
    this->getView()->setColor(CAColor_black);
     
    //创建Button
    CAButton* imageBtn = CAButton::createWithCenter(DRect(size.width*0.5, 500, 200, 50), CAButtonTypeSquareRect);
     
    //设置Buttion文本
    imageBtn->setTitleForState(CAControlStateAll, "Click");
     
    //设置tag值
    imageBtn->setTag(1);
     
    //设置按钮监听
    imageBtn->addTarget(this, CAControl_selector(FirstViewController::respondTouch), CAControlEventTouchUpInSide);
     
    //添加到屏幕
    this->getView()->addSubview(imageBtn);
}
 
void FirstViewController::respondTouch(CAControl* btn, DPoint point)
{
    //获得屏幕大小
    DSize size = this->getView()->getBounds().size;
     
    //创建CAAlerView 并设置显示文本和 green按钮和yellow按钮
    CAAlertView* alertView = CAAlertView::createWithText("ButtonImage", UTF8("点击替换按钮颜色"), "green", "yellow", NULL);
     
    //获得0-1之间的随机数
    float randNum = CCRANDOM_0_1();
     
    if (randNum > 0.333f)
    {
        //添加按钮设置文本为orange
        alertView->addButton("orange");
    }
    if (randNum> 0.666f)
    {
        //添加按钮并设置文本为blue
        alertView->addButton("blue");
    }
     
    //显示弹窗(如果不调用,弹窗不显示)
    alertView->show();
     
    //设置弹窗按钮的回调
    alertView->setTarget(this, CAAlertView_selector(FirstViewController::alertViewCallback));
}
 
void FirstViewController::alertViewCallback(int btnIndex)
{
    //根据tag获得imageBtn对象
    CAButton* imageBtn =(CAButton*) this->getView()->getSubviewByTag(1);
     
    //根据CAAlertView上按钮的index判断响应的逻辑
    if (btnIndex == 0)
    {                   
        //设置imageBtn背景色为green
        imageBtn->setBackGroundViewForState(CAControlStateNormal, CAView::createWithColor(CAColor_green));
    }
    else if (btnIndex == 1)
    {
        //设置imageBtn背景色为yellow
        imageBtn->setBackGroundViewForState(CAControlStateNormal, CAView::createWithColor(CAColor_yellow));
    }
    else if (btnIndex == 2)
    {
        //设置imageBtn背景色为orange
        imageBtn->setBackGroundViewForState(CAControlStateNormal, CAView::createWithColor(CAColor_orange));
    }
    else
    {
        //设置imageBtn背景色为blue
        imageBtn->setBackGroundViewForState(CAControlStateNormal, CAView::createWithColor(CAColor_blue));
    }
}

CAAlertView 方法说明

void setMessageFontName(std::string &var);

返回值:void

参数:

类型 参数名 说明
std::string var 信息文字

解释:设置提示信息的字体

void setTitle(std::string var, CAColor4B col);

返回值:void

参数:

类型 参数名 说明
CAColor4B col 标题颜色
std::string var 信息文字

解释:设置提示框的标题

void setAlertMessage(std::string var, CAColor4B col);

返回值:void

参数:

类型 参数名 说明
CAColor4B col 提示信息颜色
std::string var 提示信息字体

解释:设置提示框的提示信息

void addButton(const std::string& btnText, CAColor4B col = ccc4(3, 100, 255, 255), CAImage* pNormalImage = NULL,AImage* pHighlightedImage = NULL);

返回值:void

参数:

类型 参数名 说明
std::string& btnText 按钮文字
CAColor4B col 按钮颜色
CAImage* pNormalImage 按钮图像
AImage* pHighlightedImage 高亮度图像

解释:添加一个按钮到CAAlertView

void setTarget(CAObject* target, SEL_CAAlertBtnEvent selector);

返回值:void

参数:

类型 参数名 说明
CAObject* target 监听目标
SEL_CAAlertBtnEvent selector 监听选择器

解释:设置 CAAlertView 的监听

void show();

返回值:void

参数:

解释:设置显示提示框


void hide();

返回值:void

参数:

解释:设置隐藏提示框void


static bool hideWithDisplayed();

返回值:static bool

参数:

解释:隐藏提示框


static CAAlertView* create();

返回值:static CAAlertView*

参数:

解释:创建

static CAAlertView* createWithText(const char* pszTitle, const char* pszAlertMsg, const char* pszBtnText, …);

返回值:CAAlertView*

参数:

类型 参数名 说明
const char* pszTitle 标题
const char* pszAlertMsg 提示框内容
const char* pszBtnText 按钮文本

解释:创建,并指定其Text

bool initWithText(const char* szTitle, const char* szAlertMsg, const char* pszBtnText, …);

返回值:bool

参数:

类型 参数名 说明
const char* szTitle 标题
const char* szAlertMsg 提示框内容
const char* pszBtnText 按钮文本

解释:初始化并指定化文本

void addButton(CAButton* pBtn);

返回值:void

参数:

类型 参数名 说明
CAButton* pBtn 按钮

解释:添加按钮到CAAlertView

唐伯虎点蚊香

前端小白,想各位学习!

Share
Published by
唐伯虎点蚊香

Recent Posts