Usages of cceditbox text input boxes in cocos2d-x _c language

Source: Internet
Author: User
Tags addchild

Text input Box This is something that you can always use, no matter what game you do, and today we're going to see how this thing is used. The text input box is also part of the expansion library, so you know how to do it. When the user wants to enter the content in the text box, this series of procedures we need some function call to get what we want, the class containing these functions needs to implement cceditboxdelegate this interface, let's look at how to use the specific.

#ifndef __helloworld_scene_h__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h"//need to include an extension library #include "cocos-ext.h" usi
ng namespace Cocos2d;
using namespace Cocos2d::extension;
Use Cceditbox must inherit from Cceditboxdelegate interface, implement some of its functions class Helloworld:public cocos2d::cclayer,public cceditboxdelegate { Public://Here ' s a difference.

  Method ' init ' in cocos2d-x returns BOOL, instead of returning ' IDs ' in Cocos2d-iphone virtual bool init ();

  There ' s no ' id ' in CPP, so we recommend returning the class instance pointer static cocos2d::ccscene* scene ();

	Implement the "Static node ()" Method manually Create_func (HelloWorld);

	The function to implement is the following//when the keyboard pop-up edit box gets the focus, the virtual void Editboxeditingdidbegin (cceditbox* editbox) is invoked;

	Call virtual void Editboxeditingdidend (cceditbox* editbox) when the keyboard disappears when the edit box loses focus;

	Invoke virtual void editboxtextchanged (cceditbox* editbox, const std::string& text) when the text of the edit box changes.

The virtual void Editboxreturn (cceditbox* editbox) is invoked when the return key is pressed or when an area other than the keyboard is clicked; Private:ccsize m_size;
Cceditbox * EditBox;

}; #endif//__helloworld_scene_h__
BOOL Helloworld::init () {////////////////////////////////1. Super init-A-if (!
  Cclayer::init ()) {return false;

	} this->m_size = Ccdirector::shareddirector ()->getvisiblesize (); The first argument is the size of the text box, the second is the background picture of the text box under normal circumstances, the third parameter is the background picture when the background picture//fourth parameter is not available, the latter three parameters can omit EditBox = Cceditbox::create (ccsize
	(300,40), Ccscale9sprite::create ("9.9.png"), Ccscale9sprite::create ("8.9.png"));
	Editbox->setposition (CCP (M_SIZE.WIDTH/2,M_SIZE.HEIGHT/2));
	This->addchild (EditBox);

	Set preset text editbox->setplaceholder ("Please input:");

	Sets the color of the text font editbox->setfontcolor (CCC3 (255,0,0));

	Set the maximum length, supposedly this place is the length of the input box text, but not in the Win32, when porting to Android is useful editbox->setmaxlength (1); Setinputmode () Sets the input type, which can include several//keditboxinputmodeany: Open any text input keyboard, including line break//KEDITBOXINPUTMODEEMAILADDR: Open Mail Address input type keyboard//keditboxinputmodenumeric: Turn on digital symbol input type keyboard//keditboxinputmodephonenumber: Open phone Number input type keyboard//KE Ditboxinputmodeurl: Open URL input type keyboard//KEdItboxinputmodedecimal: Open numeric input type keyboard, allow decimal//keditboxinputmodesingleline: Open any text input keyboard, excluding line wrapping Editbox->setinputmod

	E (Keditboxinputmodeany);

	To set the input flags, you can have several//keditboxinputflagpassword: password form input//keditboxinputflagsensitive: Sensitive data input, storage input scheme and predictive automatic completion Keditboxinputflaginitialcapsword: Capitalize the first letter of each word, accompanied by a hint//keditboxinputflaginitialcapssentence: The first letter is capitalized and is accompanied by a hint/

	/keditboxinputflaginitialcapsallcharacters: All characters are automatically capitalized Editbox->setinputflag (Keditboxinputflagpassword);

  Set the return key in the keyboard to display the characters, this porting Android did not look out Editbox->setreturntype (Kkeyboardreturntypego); Include these options//kkeyboardreturntypedefault: default to use the keyboard return type//kkeyboardreturntypedone: The default keyboard return type is "Done"//kkeyb Oardreturntypesend: The default keyboard return type is "Send"//kkeyboardreturntypesearch: The default is to use the keyboard return type of the word "Search"//kkeyboardretu
  Rntypego: The default use of the keyboard return type of "go"//write this sentence when the following four functions will be called editbox->setdelegate (this);
return true; }//Implement the following functions to see when they are called void Helloworld::editboxeditIngdidbegin (Cceditbox * editbox) {cclog ("begin!");
	Cclabelttf * TTF = cclabelttf::create ("Begin", "", 24);
	Ttf->setposition (CCP (M_SIZE.WIDTH/4,M_SIZE.HEIGHT*1/5));
This->addchild (TTF);
	} void Helloworld::editboxeditingdidend (Cceditbox * editbox) {cclog ("end!");
	Cclabelttf * TTF = cclabelttf::create ("End", "" ", 24);
	Ttf->setposition (CCP (M_SIZE.WIDTH/4,M_SIZE.HEIGHT*4/5));
This->addchild (TTF);
	} void Helloworld::editboxtextchanged (Cceditbox * editbox,const std::string & Text) {Cclog ("textchanged!");
	Cclabelttf * TTF = cclabelttf::create ("textchanged!", "" ", 24);
	Ttf->setposition (CCP (M_SIZE.WIDTH/4,M_SIZE.HEIGHT*3/5));
This->addchild (TTF);
	} void Helloworld::editboxreturn (Cceditbox * editbox) {cclog ("return");
	Cclabelttf * TTF = Cclabelttf::create ("Return", "" ", 24);
	Ttf->setposition (CCP (M_SIZE.WIDTH/4,M_SIZE.HEIGHT*2/5));

	This->addchild (TTF);
	char * str = (char *) this->editbox->gettext ();
Cclabelttf * Text = cclabelttf::create (str, "", 24);	Text->setposition (CCP (M_SIZE.WIDTH/2,M_SIZE.HEIGHT*2/5));
This->addchild (text); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.