一 : cocos2dx3.6 实现带光标的输入框
多的不说,我这个学渣,我写的代码比较搓!忍耐下吧!
CursorTextField.h
#ifndef _CursorTextField_H_#define _CursorTextField_H_#include "cocos2d.h"USING_NS_CC;class CursorTextField : public TextFieldTTF, public TextFieldDelegate , public IMEDelegate{private:// 点击开始位置Point m_beginPos;// 光标精灵Sprite * m_pCursorSprite;// 光标动画Action *m_pCursorAction;// 光标坐标Point m_cursorPos;//输入框长度float inputFrameWidth;//允许输入的最大字符数Unicodefloat inputMaxLength;int nLenCount;int codeNumType[50]; //每个字符对应的字节数量int codeCur; //当前第几个字符int startCur; //行开头字符下标int endCur; //行末尾下标// 输入框总内容std::string m_pInputText;std::string inpuText; //当前输入框内容public:CursorTextField();~CursorTextField();// staticstatic CursorTextField * textFieldWithPlaceHolder(cocos2d::Node * node , const char *placeholder, const char *fontName, float fontSize);// Layervoid onEnter();void onExit();bool init();// 初始化光标精灵void initCursorSprite(int nHeight);// TextFieldDelegatevirtual bool onTextFieldAttachWithIME(TextFieldTTF *pSender) override;virtual bool onTextFieldDetachWithIME(TextFieldTTF * pSender) override;virtual bool onTextFieldInsertText(cocos2d::TextFieldTTF* sender, const char * text, size_t nLen) override;virtual bool onTextFieldDeleteBackward(cocos2d::TextFieldTTF* sender, const char * delText, size_t nLen) override;virtual void setPosition(const Point& pos);virtual void setPosition(float &x, float &y);void setCursorPositionX(float x); // 设置光标x位置// 把光标添加到和输入框一起的层中void AddCursor(Node *node);// Layer Touchbool onTouchBegan(Touch *pTouch, Event *pEvent);void onTouchEnded(Touch *pTouch, Event *pEvent);// 判断是否点击在TextField处bool isInTextField(Touch *pTouch);// 得到TextField矩形Rect getRect();// 打开输入法void openIME();// 关闭输入法void closeIME();std::string split_text(std::string name, int len , int start);const char* getInputText();void setInpuntText(char* text);void setInputWidth(float width);void setInputMaxLength(float length);cocos2d::Node * parentNode;protected:EventListenerTouchOneByOne * listener;};#endifCursorTextField.cpp
#include "CursorTextField.h"#include "MyCharSet.h"const static float DELTA = 0.5f;using namespace cocos2d;using namespace std;CursorTextField::CursorTextField(){TextFieldTTF();m_pCursorSprite = NULL;}CursorTextField::~CursorTextField(){}void CursorTextField::onEnter(){TextFieldTTF::onEnter();listener = EventListenerTouchOneByOne::create();listener->setSwallowTouches(true);listener->onTouchBegan = CC_CALLBACK_2(CursorTextField::onTouchBegan, this);listener->onTouchEnded = CC_CALLBACK_2(CursorTextField::onTouchEnded, this);Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);this->setDelegate(this);}CursorTextField * CursorTextField::textFieldWithPlaceHolder(Node * node , const char *placeholder, const char *fontName, float fontSize){auto pRet = new CursorTextField();pRet->parentNode = node;if (pRet && ((TextFieldTTF*)pRet)->initWithPlaceHolder(placeholder, fontName, fontSize)){pRet->autorelease();if (placeholder){pRet->setPlaceHolder(placeholder);}pRet->init();pRet->initCursorSprite(fontSize);pRet->setHorizontalAlignment(kCCTextAlignmentLeft);return pRet;}CC_SAFE_DELETE(pRet);return NULL;}bool CursorTextField::init(){this->inputFrameWidth = 400;this->inputMaxLength = 16;this->nLenCount = 0;memset(codeNumType, 0, sizeof(codeNumType));this->codeCur = 0;this->startCur = 0;this->endCur = 0;inpuText = "";return true;}void CursorTextField::initCursorSprite(const int mHeight){int column = 2;int nHeight = 20;int pixels[50][2];for (int i = 0; iinitWithData(pixels, 20, Texture2D::PixelFormat::RGB888, 4, mHeight, CCSizeMake(column, nHeight));m_pCursorSprite = Sprite::createWithTexture(texture);texture->autorelease();if (m_pCursorSprite == nullptr){CCLOG("NULL");}Size winSize = getContentSize();m_pCursorSprite->setVisible(false);parentNode->addChild(m_pCursorSprite);m_pCursorAction = RepeatForever::create(Sequence::create(FadeOut::create(0.25f), FadeIn::create(0.25f), NULL));m_pCursorSprite->runAction(m_pCursorAction);}void CursorTextField::setPosition(float &x, float &y){Point posi(x, y);setPosition(posi);}void CursorTextField::setPosition(const Point& pos){TextFieldTTF::setPosition(pos);// 设置光标位置if (NULL != m_pCursorSprite){Size winSize = getContentSize();m_cursorPos = ccp(0, 0/*winSize.height / 2*/);m_cursorPos = m_cursorPos + pos;m_pCursorSprite->setPosition(m_cursorPos.x, m_cursorPos.y + m_pCursorSprite->getContentSize().height / 2.0);}}void CursorTextField::setCursorPositionX(float x) // 设置光标x位置{Point pt = getPosition(); // 获取输入框位置m_pCursorSprite->setPositionX(pt.x + x);}// 把光标添加到和输入框一起的层中void CursorTextField::AddCursor(Node *node){if (NULL != node && NULL != m_pCursorSprite){node->addChild(m_pCursorSprite);m_pCursorSprite->setPositionY(getContentSize().height / 2.0);m_pCursorSprite->runAction(m_pCursorAction);}}bool CursorTextField::onTouchBegan(cocos2d::Touch *pTouch, cocos2d::Event *pEvent){m_beginPos = pTouch->getLocation();return true;}Rect CursorTextField::getRect(){Size size = getContentSize();return CCRectMake(0, -size.height / 2, inputFrameWidth, size.height);}//获取输入框内容const char* CursorTextField::getInputText(){const char* text = m_pInputText.c_str();return text;}//设置输入框内容void CursorTextField::setInpuntText(char* text){m_pInputText = "";setString(text);m_pCursorSprite->setPositionX(this->getPosition().x);memset(codeNumType, 0, sizeof(codeNumType));codeCur = 0;startCur = 0;endCur = 0;inpuText = "";}//设置输入框宽度 一旦字符串宽度超度这个长度 字符串会自动向左缩进void CursorTextField::setInputWidth(float width){this->inputFrameWidth = width;}//设置输入宽显示的最大字符数量Unicodevoid CursorTextField::setInputMaxLength(float length){this->inputMaxLength = length;}//判断点击事件,是否响应在输入框范围内bool CursorTextField::isInTextField(cocos2d::Touch *pTouch){return getRect().containsPoint(convertTouchToNodeSpaceAR(pTouch));}void CursorTextField::onTouchEnded(cocos2d::Touch *pTouch, cocos2d::Event *pEvent){Point endPos = pTouch->getLocation();// 判断是否为点击事件if (::abs(endPos.x - m_beginPos.x)>DELTA ||::abs(endPos.y - m_beginPos.y)){// 不是点击事件m_beginPos.x = m_beginPos.y = -1;return;}// 判断是打开输入法还是关闭输入法isInTextField(pTouch) ? openIME() : closeIME();}//弹出手机键盘时响应事件bool CursorTextField::onTextFieldAttachWithIME(cocos2d::TextFieldTTF *pSender){if (m_pInputText.empty()) {return false;}m_pCursorSprite->setPositionX(this->getPosition().x + getContentSize().width);return false;}//当有输入进来时响应//@param pSender 发送事件对象//@param text 输入内容//@param 内容字节长度bool CursorTextField::onTextFieldInsertText(cocos2d::TextFieldTTF* sender, const char * text, size_t nLen){int j = 0 ;std::string mytext = text;for (j = 0; j utf8_to_unicode((uint8_t*)t, (uint16_t **)wText, &unisize);std::string ss = split_text(mytext, 1 , j);j += ss.length();//当字符数量超过规定值 不做处理if (cou>= inputMaxLength){CC_SAFE_DELETE_ARRAY(wText);return true;}//return true;//屏蔽回车输入if (ss == "\n"){CC_SAFE_DELETE_ARRAY(wText);continue;}//输入框总内容添加m_pInputText.append(ss);//输入框当前字符串添加inpuText.append(ss);//当前字符的长度codeNumType[codeCur++] = ss.length();std::string localText = m_pInputText;setString(m_pInputText);//如果总字符串的长度大于指定宽度if (getContentSize().width>inputFrameWidth){//大于,截取字符串,直到字符串的长度小于指定宽度为止setString(inpuText);while (getContentSize().width>inputFrameWidth){int nnLen = ss.length();if (codeNumType[startCur] == 1){nnLen = 1;}if (codeNumType[startCur] == 3){nnLen = 3;}startCur++;nLenCount += nnLen;float gap = localText.size() - nLenCount;inpuText = localText.substr(nLenCount, gap);setString(inpuText.c_str());float coWidth = getContentSize().width;}}else{//小于,直接设置显示总字符串nLenCount = 0;startCur = 0;setString(m_pInputText);}//设置光标位置m_pCursorSprite->setPositionX(this->getPosition().x + getContentSize().width);CC_SAFE_DELETE_ARRAY(wText);//AndroidShowtext(mychar, 1);}return true;}//当有输入进来时响应//@param pSender 发送事件对象//@param text 删除内容//@param 内容字节长度bool CursorTextField::onTextFieldDeleteBackward(cocos2d::TextFieldTTF* sender, const char * delText, size_t nLen){// 将总字符串长度减去nLen字节长m_pInputText.resize(m_pInputText.size() - nLen);//当前字符数减一codeNumType[codeCur--] = 0;std::string localText = m_pInputText;setString(m_pInputText);if (getContentSize().width>inputFrameWidth){//大于指定宽度,截取字符串,直到字符串长度小于指定宽度while (getContentSize().width>inputFrameWidth){int nnLen = nLen;if (codeNumType[startCur - 1] == 1){nnLen = 1;}if (codeNumType[startCur - 1] == 3){nnLen = 3;}nLenCount -= nnLen;startCur--;if (startCur<= 0)startCur = 0;if (nLenCount<= 0)nLenCount = 0;float gap = localText.size() - nLenCount;const std::string text = localText.substr(nLenCount, gap);setString(text);inpuText = text;}}else{nLenCount = 0;startCur = 0;setString(m_pInputText.c_str());}//设置光标位置m_pCursorSprite->setPositionX(this->getPosition().x + getContentSize().width);if (m_pInputText.empty()) {m_pCursorSprite->setPositionX(this->getPosition().x);}return true;}bool CursorTextField::onTextFieldDetachWithIME(cocos2d::TextFieldTTF *pSender){return false;}void CursorTextField::openIME(){m_pCursorSprite->setVisible(true);setString(m_pInputText);((TextFieldTTF *)this)->attachWithIME();}void CursorTextField::closeIME(){m_pCursorSprite->setVisible(false);//auto pTextField = (TextFieldTTF *)pRet;((TextFieldTTF *)this)->detachWithIME();}void CursorTextField::onExit(){TextFieldTTF::onExit();Director::getInstance()->getEventDispatcher()->removeEventListener(listener);}std::string CursorTextField::split_text(std::string name, int len , int start){int i = start;std::string str;if (name[i]<0){i++;}//while (name[i]<0)//{//i++;//if (i - start == 2)//{//break;//}//}if (start == i){str = name.substr(start, 1);}else{str = name.substr(start, 3);}//log("mysubstr %s", str.c_str());return str;}
MyCharSet.h
#pragma once#include "stdint.h"#include "stdio.h"#include#include #include class MyCharSet{public:MyCharSet();~MyCharSet();static MyCharSet * getinstence();static void destoryinstence();int utf8_to_unicode(uint8_t *in, uint16_t **out, int *outsize);int unicode_to_utf8(uint16_t *in, int insize, uint8_t **out);};
MyCharSet.cpp
#include "MyCharSet.h"static MyCharSet *_MyCharSet = NULL;MyCharSet::MyCharSet(){}MyCharSet::~MyCharSet(){}MyCharSet * MyCharSet::getinstence(){if (_MyCharSet == NULL){_MyCharSet = new MyCharSet();}return _MyCharSet;}void MyCharSet::destoryinstence(){if (_MyCharSet != NULL){delete _MyCharSet;}}int MyCharSet::utf8_to_unicode(uint8_t *in, uint16_t **out, int *outsize){uint8_t *p = in;uint16_t *result = NULL;int resultsize = 0;uint8_t *tmp = NULL;result = (uint16_t *)malloc(strlen((char *)in) * 2 + 2); /* should be enough */memset(result, 0, strlen((char*)in) * 2 + 2);tmp = (uint8_t *)result;while (*p){if (*p>= 0x00 && *p<= 0x7f){*tmp = *p;tmp++;*tmp = '\0';resultsize += 1;}else if ((*p & (0xff<<5)) == 0xc0){uint16_t t = 0;uint8_t t1 = 0;uint8_t t2 = 0;t1 = *p & (0xff>>3);p++;t2 = *p & (0xff>>2);*tmp = t2 | ((t1 & (0xff>>6))<<6);//t1>>2;tmp++;*tmp = t1>>2;//t2 | ((t1 & (0xff>>6))<<6);tmp++;resultsize += 1;}else if ((*p & (0xff<<4)) == 0xe0){uint16_t t = 0;uint8_t t1 = 0;uint8_t t2 = 0;uint8_t t3 = 0;t1 = *p & (0xff>>3);p++;t2 = *p & (0xff>>2);p++;t3 = *p & (0xff>>2);//Little Endian*tmp = ((t2 & (0xff>>6))<<6) | t3;//(t1<<4) | (t2>>2);tmp++;*tmp = (t1<<4) | (t2>>2);//((t2 & (0xff>>6))<<6) | t3;tmp++;resultsize += 1;}p++;}//*tmp = '\0';//tmp++;//*tmp = '\0';//resultsize += 2;//*out = result;//*outsize = resultsize;return resultsize;}int MyCharSet::unicode_to_utf8(uint16_t *in, int insize, uint8_t **out){int i = 0;int outsize = 0;int charscount = 0;uint8_t *result = NULL;uint8_t *tmp = NULL;charscount = insize / sizeof(uint16_t);result = (uint8_t *)malloc(charscount * 3 + 1);memset(result, 0, charscount * 3 + 1);tmp = result;for (i = 0; i= 0x0000 && unicode<= 0x007f){*tmp = (uint8_t)unicode;tmp += 1;outsize += 1;}else if (unicode>= 0x0080 && unicode<= 0x07ff){*tmp = 0xc0 | (unicode>>6);tmp += 1;*tmp = 0x80 | (unicode & (0xff>>2));tmp += 1;outsize += 2;}else if (unicode>= 0x0800 && unicode<= 0xffff){*tmp = 0xe0 | (unicode>>12);tmp += 1;*tmp = 0x80 | (unicode>>6 & 0x00ff);tmp += 1;*tmp = 0x80 | (unicode & (0xff>>2));tmp += 1;outsize += 3;}}*tmp = '\0';*out = result;return outsize;}
如果使用直接:
CursorTextField *m_pCursorInputLayer = CursorTextField::textFieldWithPlaceHolder(this, "click and input","fonts/Marker Felt.ttf", 40);m_pCursorInputLayer->setAnchorPoint(ccp(0.0f, 0.0f));m_pCursorInputLayer->setInputWidth(visibleSize.width);Vec2 posi(0, visibleSize.height / 2);m_pCursorInputLayer->setPosition(posi);this->addChild(m_pCursorInputLayer, 1);我这个输入解决了Android上一堆输入导致的错误,大家可以仔细去看看!
给个思路大家吧,在切割字符的时候可以自行添加回车,我暂时想到这样来做!
我做的效果!
二 : Win10安全:如何去掉登录密码输入框明文显示功能?
为了方便用户输入密码,Win10登录界面的密码输入框右端有显示明文的按钮(眼睛图案),按住不松手就可以显示密码的明文内容。这项功能可以让用户有机会检查密码是否输入正确。不过凡事皆有利弊,在你能够查看密码的同时,如果你周围有别有用心之人,他们也能够顺便看到你的密码,比较特殊的情况就是你输密码之后有事离开,他们就可以点一下查看按钮来获取密码。
如果你所处的环境较为险恶,那么可以考虑直接去掉这项功能,不给他人可乘之机。想去掉这项功能也十分简单,添加一个注册表值就可以完成任务,具体操作如下:
1、在Cortana搜索栏上输入regedit,按回车键进入注册表编辑器
2、定位到
HKEY_LOCAL_MACHINESoftwarePoliciesMicrosoftWindows
3、新建项CredUI,如下图——
4、新建DWORD(32位)值,命名为DisablePasswordReveal,如下图——
5、双击修改数值数据为1,点击“确定”后生效,如下图——
此时可尝试锁屏,然后用密码输入方式进行解锁,发现输入框右端的明文显示按钮已经消失,密码只能显示为圆点(如下图)。当然,这样一来别人肯定是看不到你的密码了,因为你自己也看不到了,如果你觉得不方便,可以把DisablePasswordReveal删掉,恢复默认的显示功能。
三 : 在php中输出html代码
<?php四 : html禁止文本框输入
禁止文本框输入的几个常用标签input中有几个属性,分别是readonly、disabled、autocomplete,下面还是来看看这几个标签的使用。
一、html禁止input文本框输入
readonly 属性规定输入字段为只读。只读字段是不能修改的。不过,用户仍然可以使用tab键切换到该字段,还可以选中或拷贝其文本。
<input readonly="readonly" value="xxxx"/>
加入type="text属性的代码编写:
<input type="text" name="www.xxx" readonly="readonly" />
disabled 属性规定应该禁用 input 元素。被禁用的 input 元素既不可用,也不可点击,也是不能选择。可以设置 disabled 属性,直到满足某些其他的条件为止(比如选择了一个复选框等等)。当然,我们也可以就需要通过 JavaScript 来删除disabled值,将input元素的值切换为可用。
<input disabled="disabled" value="xxxx"/>
加入type="text属性的代码编写:
<input type="text" name="www.61k.comquot; disabled="disabled" />
二、css屏蔽input不输入:css的代码如下:
<input style="ime-mode: disabled">
三、前面有涉及到autocomplete这个属性,其实这个属性也是可以这里就运用到让文本框禁止输入上。具体代码如下:
<input type="text" autocomplete="off" id="number"/>
autocomplete解释:input 的属性autocomplete 默认为on。其含义代表是否让浏览器自动记录之前输入的值。很多时候,需要对客户的资料进行保密,防止浏览器软件或者恶意插件获取到。可以在input中加入autocomplete="off" 来关闭记录,所以说如果是不让input输入或者对系统需要保密的情况下可以使用此参数。
本文标题:html密码输入框明文-cocos2dx3.6 实现带光标的输入框61阅读| 精彩专题| 最新文章| 热门文章| 苏ICP备13036349号-1