当前位置:K88软件开发文章中心编程语言APP编程CrossApp → 文章内容

CAAlertView(提示框)

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-23 13:51:49

由 ?﹏???ζ???﹏﹏? 创建, 最后一次修改 2016-08-29 类说明CAAlertView是提示框控件,如果提示框内的按钮个数不超过三个,这个横向排列按钮,如果按钮个数超过三个则纵向排列。CAAlertView 方法 (点击方法名可查看方法介绍)方法说明addButton添加一个按钮到CAAlertViewsetAlertMessage提示框的提示信息 hide隐藏提示框setMessageFontName提示信息的字体 show显示提示框setTarget添加监听 setTitle提示框的标题hideWithDisplayed隐藏提示框create创建createWithText创建,并指定其TextinitWithText初始化,并指定化文本addButton添加按钮到CAAlertViewCAAlertView是提示框控件,如果提示框内的按钮个数不超过三个,这个横向排列按钮,如果按钮个数超过三个则纵向排列。我们来看一下实例代码:首先在.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::stringvar信息文字解释:设置提示信息的字体void setTitle(std::string var, CAColor4B col);返回值:void参数:类型参数名说明CAColor4Bcol标题颜色std::stringvar信息文字解释:设置提示框的标题void setAlertMessage(std::string var, CAColor4B col);返回值:void参数:类型参数名说明CAColor4Bcol提示信息颜色std::stringvar提示信息字体解释:设置提示框的提示信息void addButton(const std::string& btnText, CAColor4B col = ccc4(3, 100, 255, 255), CAImage* pNormalImage = NULL,AImage* pHighlightedImage = NULL);返回值:void参数:类型参数名说明std::string&btnText按钮文字CAColor4Bcol按钮颜色CAImage*pNormalImage按钮图像AImage*pHighlightedImage高亮度图像解释:添加一个按钮到CAAlertViewvoid setTarget(CAObject* target, SEL_CAAlertBtnEvent selector);返回值:void参数:类型参数名说明CAObject*target监听目标SEL_CAAlertBtnEventselector监听选择器解释:设置 CAAlertView 的监听void show();返回值:void参数:解释:设置显示提示框void hide();返回值:void参数:解释:设置隐藏提示框voidstatic 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按钮文本解释:创建,并指定其Textbool 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

CAAlertView(提示框)