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

CATextField(输入框1.2以前版本)

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

由 ?﹏???ζ???﹏﹏? 创建, 最后一次修改 2016-09-08 类说明CATextField是单行输入框控件。主要接收用户的文本输入,多用于用户名、密码、聊天输入等。(1.1版本)CATextField 属性 (点击属性名可查看属性介绍)属性说明BackgroundViewTextField的背景视图CursorColorTextField的光标颜色FontNameTextField的字体名称FontSizeTextField的字体大小HoriMarginsTextField的水平边缘InputTypeTextField的输入类型PlaceHolderPlaceHolder文本内容SpaceHolderColorPlaceHolder文本内容颜色TextTextField的文本内容TextColorTextField的文字颜色TextEditAlignTextField的文本编辑对齐CharCountTextField的字符计数DelegateTextField的代理(设置代理才能被监听状态)VertMarginsTextField的垂直边缘CATextField 方法 (点击方法名可查看方法介绍)函数说明setKeyboardType设置键盘的类型(真机或模拟器上有效)getKeyboardType获取键盘类型(真机或模拟器上有效)setKeyboardReturnType设置确认键的类型(真机或模拟器上有效)  getKeyboardReturnType获取确认键的类型(真机或模拟器上有效)  resignFirstResponder隐藏键盘第一响应者状态becomeFirstResponder弹出键盘第一响应者状态resignResponder隐藏键盘状态createWithFrame创建,并指定其FramecreateWithCenter创建,并指定其Centerinit初始化setImageRect设置图像大小updateImageRect更新图像setColor设置颜色getColor获取颜色CATextField是单行输入框控件。主要接收用户的文本输入,多用于用户名、密码、聊天输入等。在CATextField接受用户输入文本时,我们有时候希望获得用户的操作行为,比如CATextField获得焦点、CATextField失去焦点、用户输入字符、用户删除字符,这样我们可以对用户的操作进行逻辑处理,比如限制输入内容,输入字符长度等。那么如何才能监听到CATextField的改变呢?我们需要了解一下啊CATextFieldDelegate,它主要使用的有四个函数分别是://获得焦点 virtual bool onTextFieldAttachWithIME(CATextField * sender); //失去焦点 virtual bool onTextFieldDetachWithIME(CATextField * sender); //输入文本 virtual bool onTextFieldInsertText(CATextField * sender, const char * text, int nLen); //删除文本 virtual bool onTextFieldDeleteBackward(CATextField * sender, const char * delText, int nLen)假如我们想在FirstViewController中监听CATextField那么我们需要使FirstViewController继承CATextFieldDelegate并重写这些函数。下面我们以常见的登陆界面为例,来讲解CATextField的使用方法。首先看FirstViewController.h文件代码#include <iostream>#include "CrossApp.h" USING_NS_CC; class FirstViewController : public CAViewController, public CATextFieldDelegate{ public: FirstViewController(); virtual ~FirstViewController(); //获得焦点 virtual bool onTextFieldAttachWithIME(CATextField * sender); //失去焦点 virtual bool onTextFieldDetachWithIME(CATextField * sender); //输入文本 virtual bool onTextFieldInsertText(CATextField * sender, const char * text, int nLen); //删除文本 virtual bool onTextFieldDeleteBackward(CATextField * sender, const char * delText, int nLen); //登录 void login(CAControl* control,DPoint point); protected: void viewDidLoad(); void viewDidUnload(); };然后我们在FirstViewController.cpp中做逻辑实现#include "FirstViewController.h"FirstViewController::FirstViewController(){} FirstViewController::~FirstViewController(){} void FirstViewController::viewDidLoad(){ //用户名文本 CALabel* nameLabel = CALabel::createWithFrame(DRect(50, 100, 100, 40)); nameLabel->setText(UTF8("用户名:")); nameLabel->setTextAlignment(CATextAlignmentCenter); nameLabel->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter); this->getView()->addSubview(nameLabel); //密码文本 CALabel* passwordLabel = CALabel::createWithFrame(DRect(50, 200, 100, 40)); passwordLabel->setText(UTF8("密码:")); passwordLabel->setTextAlignment(CATextAlignmentCenter); passwordLabel->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter); this->getView()->addSubview(passwordLabel); //创建 CATextField* nameTF = CATextField::createWithFrame(DRect(200, 100, 300, 40)); //设置tag nameTF->setTag(1); //设置提示文本 nameTF->setPlaceHolder(UTF8("请输入用户名")); //设置光标颜色 nameTF->setCursorColor(CAColor_orange); /*设置键盘类型(真机或模拟器上有效) KEY_BOARD_TYPE_NORMAL:普通键盘 KEY_BOARD_TYPE_NUMBER:数字键盘 KEY_BOARD_TYPE_ALPHABET:字母键盘 */ nameTF->setKeyboardType(eKeyBoardType::KEY_BOARD_TYPE_ALPHABET); /*设置确认键的类型(真机或模拟器上有效) KEY_BOARD_RETURN_DONE:完成 KEY_BOARD_RETURN_SEARCH:搜索 KEY_BOARD_RETURN_SEND:发送 */ nameTF->setKeyboardReturnType(eKeyBoardReturnType::KEY_BOARD_RETURN_DONE); //绑定代理(设置代理才能被监听状态) nameTF->setDelegate(this); //添加渲染 this->getView()->addSubview(nameTF); CATextField* password = CATextField::createWithFrame(DRect(200,200,300,40)); //设置tag password->setTag(2); //设置提示文本 password->setPlaceHolder(UTF8("请输入密码")); //设置提示文本颜色 password->setSpaceHolderColor(CAColor_red); //设置输入样式为密码格式 password->setInputType(eKeyBoardInputType::KEY_BOARD_INPUT_PASSWORD); //添加渲染 this->getView()->addSubview(password); //登录按钮 CAButton

[1] [2]  下一页


CATextField(输入框1.2以前版本)