课程表

入门与环境搭建

简单控件的使用

视图控制

调用、存储、网络和动画

其他控件的使用

工具箱
速查手册

CAAlertView(提示框)

当前位置:免费教程 » 移动开发 » CrossApp

类说明

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


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

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


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


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

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

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

  1. void FirstViewController::viewDidLoad()
  2. {
  3. //获取屏幕宽度
  4. DSize size = this->getView()->getBounds().size;
  5. //设置背景颜色为黑色
  6. this->getView()->setColor(CAColor_black);
  7. //创建Button
  8. CAButton* imageBtn = CAButton::createWithCenter(DRect(size.width*0.5, 500, 200, 50), CAButtonTypeSquareRect);
  9. //设置Buttion文本
  10. imageBtn->setTitleForState(CAControlStateAll, "Click");
  11. //设置tag值
  12. imageBtn->setTag(1);
  13. //设置按钮监听
  14. imageBtn->addTarget(this, CAControl_selector(FirstViewController::respondTouch), CAControlEventTouchUpInSide);
  15. //添加到屏幕
  16. this->getView()->addSubview(imageBtn);
  17. }
  18. void FirstViewController::respondTouch(CAControl* btn, DPoint point)
  19. {
  20. //获得屏幕大小
  21. DSize size = this->getView()->getBounds().size;
  22. //创建CAAlerView 并设置显示文本和 green按钮和yellow按钮
  23. CAAlertView* alertView = CAAlertView::createWithText("ButtonImage", UTF8("点击替换按钮颜色"), "green", "yellow", NULL);
  24. //获得0-1之间的随机数
  25. float randNum = CCRANDOM_0_1();
  26. if (randNum > 0.333f)
  27. {
  28. //添加按钮设置文本为orange
  29. alertView->addButton("orange");
  30. }
  31. if (randNum> 0.666f)
  32. {
  33. //添加按钮并设置文本为blue
  34. alertView->addButton("blue");
  35. }
  36. //显示弹窗(如果不调用,弹窗不显示)
  37. alertView->show();
  38. //设置弹窗按钮的回调
  39. alertView->setTarget(this, CAAlertView_selector(FirstViewController::alertViewCallback));
  40. }
  41. void FirstViewController::alertViewCallback(int btnIndex)
  42. {
  43. //根据tag获得imageBtn对象
  44. CAButton* imageBtn =(CAButton*) this->getView()->getSubviewByTag(1);
  45. //根据CAAlertView上按钮的index判断响应的逻辑
  46. if (btnIndex == 0)
  47. {
  48. //设置imageBtn背景色为green
  49. imageBtn->setBackGroundViewForState(CAControlStateNormal, CAView::createWithColor(CAColor_green));
  50. }
  51. else if (btnIndex == 1)
  52. {
  53. //设置imageBtn背景色为yellow
  54. imageBtn->setBackGroundViewForState(CAControlStateNormal, CAView::createWithColor(CAColor_yellow));
  55. }
  56. else if (btnIndex == 2)
  57. {
  58. //设置imageBtn背景色为orange
  59. imageBtn->setBackGroundViewForState(CAControlStateNormal, CAView::createWithColor(CAColor_orange));
  60. }
  61. else
  62. {
  63. //设置imageBtn背景色为blue
  64. imageBtn->setBackGroundViewForState(CAControlStateNormal, CAView::createWithColor(CAColor_blue));
  65. }
  66. }

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高亮度图像

解释:添加一个按钮到CAAlertView


void setTarget(CAObject* target, SEL_CAAlertBtnEvent selector);

返回值:void

参数:

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

解释:设置 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

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号