Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

native textfield does not working
Started by nghiatran Jan 01 2015 08:53 PM

10 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

nghiatran

[GLOBAL: userInfoPane.html]
nghiatran
  • Observer

  • 8 posts
  • Corona SDK

Hi all

I can't input anything text when work with native textfield
please help me
 


[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,786 posts
  • Enterprise

You need to help us help you!

 

What version of Corona SDK are you using

What platform are you developing on (Mac or Windows)

Are you having the problem in the simulator or on a Device?

What device are you trying to build for (if you are yet)

 

Are you getting any errors in your device's console log?  Don't know how to read that? http://coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

 

Rob



[TOPIC: post.html]
#3

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

Are you talking about Windows Phone?

If so, then native TextFields or TextBoxes are not supported.

 

Please see the following page on what features are supported on WP8:

   http://docs.coronalabs.com/daily/coronacards/wp8/support.html



[TOPIC: post.html]
#4

nghiatran

[GLOBAL: userInfoPane.html]
nghiatran
  • Observer

  • 8 posts
  • Corona SDK

Hi

I using Corona SDK and CoronaCard on Windows But I can't input anything on wp8(Simulator and Device)

If native TextFields or TextBoxes are not supported then I should do to enter input

plz help me

thanks.. :D



[TOPIC: post.html]
#5

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

For WP8, since you are already developing natively via Visual Studio, you already have the ability to create a XAML TextBox yourself in .NET.  I recommend that you create your own native popup via C# that contains the text fields you need.  I provide some guidance on how to do this here...

   http://forums.coronalabs.com/topic/51055-keyboard-input/?p=266490



[TOPIC: post.html]
#6

nghiatran

[GLOBAL: userInfoPane.html]
nghiatran
  • Observer

  • 8 posts
  • Corona SDK

Hi 
I had created 2 textfields(not support on the CoronaCard) and 1 button on the Corona for login and then create popup(contains 2 textboxs) via c#(using Windows phone user control) that contains 2 textfields and after that I can't assign string(username and password) from 2 textboxes to 2 textfields when click button login(that is I can't login when using user control) ..please help for me example for stupid of me
Thanks so much


[TOPIC: post.html]
#7

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

You can assign a string to your .NET TextBox via its "TextBox.Text" property...

   http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.text(v=vs.110).aspx

 

You'll probably want to set up your MainPage.xaml.cs code like this...

public partial class MainPage : PhoneApplicationPage
{
	/// <summary>Reference to the Corona runtime environment that is currently running.</summary>
	private CoronaLabs.Corona.WinRT.CoronaRuntimeEnvironment fCoronaRuntimeEnvironment = null;

	/// <summary>Constructor. Initializes member variables and the user interface.</summary>
	public MainPage()
	{
		// Initialize this page's components that were set up via the UI designer.
		InitializeComponent();

		// Set up Corona to automatically start up when the control's Loaded event has been raised.
		// Note: By default, Corona will run the "main.lua" file in the "Assets\Corona" directory.
		//       You can change the defaults via the CoronaPanel's AutoLaunchSettings property.
		fCoronaPanel.AutoLaunchEnabled = true;

		// Set up the CoronaPanel control to render fullscreen via the DrawingSurfaceBackgroundGrid control.
		// This significantly improves the framerate and is the only means of achieving 60 FPS.
		fCoronaPanel.BackgroundRenderingEnabled = true;
		fDrawingSurfaceBackgroundGrid.SetBackgroundContentProvider(fCoronaPanel.BackgroundContentProvider);
		fDrawingSurfaceBackgroundGrid.SetBackgroundManipulationHandler(fCoronaPanel.BackgroundManipulationHandler);

		// Add event handlers to the Corona runtime.
		fCoronaPanel.Runtime.Loaded += OnCoronaRuntimeLoaded;
		fCoronaPanel.Runtime.Terminating += OnCoronaRuntimeExiting;
	}

	/// <summary>
	///  Called when a new CoronaRuntimeEnvironment has been created/loaded,
	///  but before the "main.lua" has been executed.
	/// </summary>
	/// <param name="sender">The CoronaRuntime object that raised this event.</param>
	/// <param name="e">Event arguments providing the CoronaRuntimeEnvironment that has been created/loaded.</param>
	private void OnCoronaRuntimeLoaded(
		object sender, CoronaLabs.Corona.WinRT.CoronaRuntimeEventArgs e)
	{
		// Keep a reference to the Corona runtime environment.
		// It's needed so that your login window's results can be dispatched to Corona.
		fCoronaRuntimeEnvironment = e.CoronaRuntimeEnvironment;

		fCoronaRuntimeEnvironment.AddEventListener("requestingLogin", OnRequestingLogin);
	}

	/// <summary>
	///  Called when the CoronaRuntimeEnvironment is terminating
	///  during a Lua "applicationExit" system event.
	/// </summary>
	/// <param name="sender">The CoronaRuntime object that raised this event.</param>
	/// <param name="e">Event arguments providing the CoronaRuntimeEnvironment that is being terminated.</param>
	private void OnCoronaRuntimeExiting(
		object sender, CoronaLabs.Corona.WinRT.CoronaRuntimeEventArgs e)
	{
		// Give up our reference to the Corona runtime since it is not running anymore.
		// This also allows it to be garbage collected.
		fCoronaRuntimeEnvironment = null;
	}

	/// <summary>Called by Lua when it is requesting a login popup to be shown.</summary>
	/// <param name="sender">The CoronaRuntimeEnvironment that dispatched this event.</param>
	/// <param name="e">Provides the Lua vent table's fields/properties.</param>
	/// <returns>Returns a boxed object to Lua.</returns>
	private CoronaLabs.Corona.WinRT.ICoronaBoxedData OnRequestingLogin(
		CoronaLabs.Corona.WinRT.CoronaRuntimeEnvironment sender,
		CoronaLabs.Corona.WinRT.CoronaLuaEventArgs e)
	{
		// Fetch the "event.loginName" property, if provided.
		string loginName = string.Empty;
		var boxedLoginName = e.Properties.Get("loginName") as CoronaLabs.Corona.WinRT.CoronaBoxedString;
		if (boxedLoginName != null)
		{
			loginName = boxedLoginName.ToString();
		}

		// Display your login popup here using the "loginName" fetched up above.

		// This returns nil to Lua.
		return null;
	}

	/// <summary>*** You should call this method when the login popup closes.***</summary>
	private void OnLoginPopupClosed()
	{
		// Do not continue if the Corona runtime is no longer running.
		if (fCoronaRuntimeEnvironment == null)
		{
			return;
		}

		// Fetch your login popup's user name and password here. This is a task for you.
		string loginName = "Name";
		string loginPassword = "Password";

		// Create a custom Corona event named "userLoggedIn" with the following properties.
		// This will be converted into a Lua "event" table once dispatched by Corona.
		var eventProperties = CoronaLabs.Corona.WinRT.CoronaLuaEventProperties.CreateWithName("userLoggedIn");
		eventProperties.Set("loginName", loginName);
		eventProperties.Set("loginPassword", loginPassword);

		// Dispatch the event to Lua.
		var eventArgs = new CoronaLabs.Corona.WinRT.CoronaLuaEventArgs(eventProperties);
		fCoronaRuntimeEnvironment.DispatchEvent(eventArgs);
	}
}

And your Lua file something like this...

-- Display text on the screen.
local text = display.newText(
				"Tap the screen to log in.",
				display.contentCenterX, display.contentCenterY,
				native.systemFont, 20)

-- Called when the user taps the screen.
-- Dispatches a "requestingLogin" event to be handled on the C# side.
local function onTappedScreen(event)
	local requestingLoginEvent =
	{
		name = "requestingLogin",
		loginName = "MyUserName",
	}
	Runtime:dispatchEvent(requestingLoginEvent)
end
Runtime:addEventListener("tap", onTappedScreen)

-- Called when the C# login popup has been closed.
local function onUserLoggedIn(event)
	-- Fetch the login name and password. Do whatever you got to do here.
	local loginName = event.loginName
	local loginPassword = event.loginPassword
end
Runtime:addEventListener("userLoggedIn", onUserLoggedIn)

The idea is that you'll dispatch an event from Lua to C# indicating that it wants a Login popup to be displayed.  The code above provides an optional "loginName" string in case you want to pre-initialize the login field.  You'll also need another event so that your C# code can provide the login results back to Lua to do whatever it needs to do.



[TOPIC: post.html]
#8

nghiatran

[GLOBAL: userInfoPane.html]
nghiatran
  • Observer

  • 8 posts
  • Corona SDK

login page of me

local userTextField
local passTextField
local function fieldEventListener(event)
if event.phase == "submitted" then
if event.target == userTextField then
native.setKeyboardFocus(passTextField)
elseif event.target == passTextField then
native.setKeyboardFocus(userTextField)
end
end
return true
end
local controlWidth = 280
local controlHeight = 40
group.gameNameImage = display.newImageRect(Util.getLocalizedImagePath("icon_cotuong.png"), 92, 36)
group.gameNameImage.x = Constants.CENTER_X
group.gameNameImage.y = -36
group:insert(group.gameNameImage)
group.loginFieldBg = display.newRoundedRect( (Constants.WIDTH)/2, 82 + controlHeight/2 , controlWidth, controlHeight*2, 5 )
group.loginFieldBg:setFillColor(0.9, 0.9, 0.9)
group:insert(group.loginFieldBg)
userTextField = TextField.newTextField(Constants.MUI.USERNAME_LABEL, textFieldX, textFieldY + group.loginFieldBg.y - controlHeight/2, textFieldWidth, Constants.TEXTFIELD_HEIGHT,textFieldWidth)
userTextField:addEventListener("userInput", fieldEventListener)
group.userTextFieldIcon = display.newImageRect(Util.getImagePath("icon_username.png"), 24, 24)
group.userTextFieldIcon.x = Constants.WIDTH/2 - 120
group.userTextFieldIcon.y = group.loginFieldBg.y - controlHeight/2
group:insert(group.userTextFieldIcon)
passTextField = TextField.newTextField(Constants.MUI.PASSWORD_LABEL, textFieldX , textFieldY + group.loginFieldBg.y + controlHeight/2, textFieldWidth , Constants.TEXTFIELD_HEIGHT)
passTextField:addEventListener("userInput", fieldEventListener)
group.passwordTextFieldIcon = display.newImageRect(Util.getImagePath("icon_key.png"), 24, 24)
group.passwordTextFieldIcon.x = group.userTextFieldIcon.x
group.passwordTextFieldIcon.y = group.loginFieldBg.y + controlHeight/2
group:insert(group.passwordTextFieldIcon)
group.seperateLineAbove = display.newRect((Constants.WIDTH)/2, group.loginFieldBg.y, controlWidth, 1)
group.seperateLineAbove:setFillColor(0.8, 0.8 , 0.8)
group:insert(group.seperateLineAbove)
group.seperateLineBelow = display.newRect((Constants.WIDTH)/2, group.loginFieldBg.y + 1, controlWidth, 1)
group.seperateLineBelow:setFillColor(1, 1 , 1)
group:insert(group.seperateLineBelow)
local function buttonEventListener(event)
if event.target == group.loginBtn then
native.setKeyboardFocus(nil)
local data = {}
data.username = userTextField.text
data.password = passTextField.text
if userTextField.text == userTextField.hintText then
data.username = ""
end
if passTextField.text == passTextField.hintText then
data.password = ""
end
local customEvent = { name = "popupEvent", target = group, type = "login", data = data }
if group ~= nil then
group:dispatchEvent(customEvent)
end
elseif event.target == group.resetBtn then
native.setKeyboardFocus(nil)
local customEvent = { name = "popupEvent", target = group, type = "reset" }
if group ~= nil then
group:dispatchEvent(customEvent)
end
elseif event.target == group.registerBtn then
native.setKeyboardFocus(nil)
local customEvent = { name = "popupEvent", target = group, type = "register" }
if group ~= nil then
group:dispatchEvent(customEvent)
end
end
return true
end

and Main page contain popup(popup contains 2 textbox)

public partial class MainPage : PhoneApplicationPage
{
private CoronaLabs.Corona.WinRT.CoronaRuntimeEnvironment fCoronaRuntimeEnvironment = null;
public MainPage()
{
var myPopup = new MyPopup();
myPopup.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
myPopup.VerticalAlignment = System.Windows.VerticalAlignment.Center;
fCoronaPanel.Children.Add(myPopup);
// Initialize this page's components that were set up via the UI designer.
InitializeComponent();
// Set up Corona to automatically start up when the control's Loaded event has been raised.
// Note: By default, Corona will run the "main.lua" file in the "Assets\Corona" directory.
// You can change the defaults via the CoronaPanel's AutoLaunchSettings property.
fCoronaPanel.AutoLaunchEnabled = true;
// Set up the CoronaPanel control to render fullscreen via the DrawingSurfaceBackgroundGrid control.
// This significantly improves the frame rate and is the only means of achieving 60 FPS.
fCoronaPanel.BackgroundRenderingEnabled = true;
fDrawingSurfaceBackgroundGrid.SetBackgroundContentProvider(fCoronaPanel.BackgroundContentProvider);
fDrawingSurfaceBackgroundGrid.SetBackgroundManipulationHandler(fCoronaPanel.BackgroundManipulationHandler);
// Add application event handlers.
App.RootFrame.Obscured += OnAppObscured;
App.RootFrame.Unobscured += OnAppUnobscured;
fCoronaPanel.Runtime.Loaded += OnCoronaRuntimeLoaded;
fCoronaPanel.Runtime.Terminating += OnCoronaRuntimeExiting;
}

I want text/string of textboxs will be load/assign into textfield(txt_user.text==userTextField.text andtxt_pass.text==passTextField.text)

and when click login button then data.username and data.password will be load string/text of txt_user.text and txt_pass.text 

please help me

:(((



[TOPIC: post.html]
#9

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

You're making this much more difficult on yourself than it needs to be.

 

First, there is no support for native.setKeyboardFocus() on WP8.  Avoid calling it.

 

Second, why are you trying to create text fields in Lua?  Corona doesn't support this on WP8.  Nor does Corona support a text field event listener.  You should display the MyPopup() user control instead and handle *all* text input on the C# side.  It'll be a lot easier this way.

 

Third, you cannot add myPopup to your "fCoronaPanel" above the InitializeComponent() method call.  Your "fCoronaPanel", along with any other XAML control that you've added in the UI designer, will be null until after the InitializeComponent() method has been called.  So, I'm pretty sure your code will cause a NullReferenceException on startup.  You'll need to create the popup and add it to fCoronaPanel below this method.



[TOPIC: post.html]
#10

nghiatran

[GLOBAL: userInfoPane.html]
nghiatran
  • Observer

  • 8 posts
  • Corona SDK

Thanks so much

I will be remove textfield and replace textbox and communication between textbox and button(in CoronaCard) 

I just 1 question, I should using native in Xaml(textbox and button) or should using textbox(user control Xaml) and button(in CoronaCard)



[TOPIC: post.html]
#11

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

You should use Microsoft's Xaml controls.  If you create a "UserControl" like in the instructions I linked to you up above, you can then use Microsoft's Visual Studio UI designer to drag-and-drop its Xaml TextBoxes and Buttons to your UserControl.  Design a login window they you want it to look like to the end-user.  I imagine you'll want to create an OK and Cancel button.  After dragging-and-dropping those buttons on to your UI, if you double click on a button in the UI designer, Visual Studio will automatically generate an OnClicked() method in your UserControl's C# code that you can use to handle the button event.  For the OK button, you should probably fetch text from your TextBoxes (user name and password), validate them, and then decide to send the login event to Lua or display an error message box to the user if something is wrong.

 

For error messages, you can use Microsoft's built-in "System.Windows.MessageBox" class' static Show() to display an error message to the end-user directly in C#.  That'll probably be easier than to set up the Lua/.NET communications to have Corona display a message box via its native.showAlert() Lua function.  Just be warned that Microsoft's message box is blocking (ie: rendering will stop) and you cannot customize the buttons (ie: limited to [OK] button... or [OK] [Cancel] buttons).

   http://msdn.microsoft.com/en-us/library/system.windows.messagebox(v=vs.110).aspx

 

Alternatively, you can use Corona's custom "CoronaLabs.Corona.WinRT.PhoneCoronaMessageBox" class which is what we use when you call our Lua native.showAlert() function.  This message box works more like how you expect it to work on Android and iOS which is non-blocking and supports multiple custom buttons.

   http://docs.coronalabs.com/daily/native/wp8/html/html/T_CoronaLabs_Corona_WinRT_Phone_CoronaMessageBox.htm




[topic_controls]
[/topic_controls]