Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

I need an idea for my project
Started by anil190201 Aug 19 2017 07:59 AM

3 replies to this topic
android menu input

Best Answer cyberparkstudios , 19 August 2017 - 05:32 PM

Here is a sample code that might do what you want.  This is just one of many ways to do the same thing.  Probably better. more efficient ways, but this way works.  Hopefully it will be good to learn from.

local w = display.contentWidth
local h = display.contentHeight
local horzMargin = w * .10
local vertMargin = h * .20
local horzSpace = 1
local vertSpace = 1
local horzCnt = 5
local vertCnt = 4
local cellWidth = (w - (horzMargin * 2)) / horzCnt
local cellHeight = cellWidth

local grid = display.newGroup()
local posX = cellWidth * .5
local posY = cellHeight * .5
local horzCursor = 0
local cellCnt = 0

local inputCnt = 0
local inputTotal = 0

local resultText
local resetButton
local resetText

local function onCellTouch(e)
	if e.phase == "ended" then
		if inputCnt < 4 and e.target.alpha == 1 then
			e.target.alpha = .5
			inputTotal = inputTotal + e.target.id
			inputCnt = inputCnt + 1
			resultText.text = inputTotal
			if inputCnt == 4 then
				resultText:setFillColor(.7, .1, .1)
			end
		end
	end
end

local function resetGrid(e)	
	if e.phase == "began" then
		e.target.alpha = .5
	elseif e.phase == "ended" then
		for i = 1, grid.numChildren do
			grid[i].alpha = 1			
		end
		resultText:setFillColor(1)
		inputTotal = 0
		inputCnt = 0
		resultText.text = "---"
		e.target.alpha = 1
	end
end

for i = 1, vertCnt do
	horzCursor = horzCursor + 1	
 for ii = 1, horzCnt do
 	local cell, cellRect, cellText
 	cellCnt = cellCnt + 1
 	cell = display.newContainer(cellWidth, cellHeight)
 	cellRect = display.newRect(0, 0, cellWidth, cellHeight)
 	cellRect:setFillColor(.7, .1, .1)
 	cellText = display.newText(cellCnt, 0, 0, nil, 24)
 	cellText:setFillColor(0)
 	cell:insert(cellRect)
 	cell:insert(cellText)
 	grid:insert(cell)
 	cell.x = posX
 	cell.y = posY
 	cell:addEventListener("touch", onCellTouch)
 	cell.id = cellCnt
 	 	
 	posX = posX + cellWidth + horzSpace
 end
 	posX = cellWidth * .5
 	posY = posY + cellHeight + vertSpace
end

-- center the 'grid' on the screen
grid.x = (w * .5) - (grid.width * .5)
grid.y = (h * .5) - (grid.height * .5)

resultText = display.newText("---", w * .5, 30, nil, 32)
resetButton = display.newRect(w * .5, h - 30, w * .25, h * .1)
resetButton:setFillColor(.5, .5, .5)
resetButton:addEventListener("touch", resetGrid)
resetText = display.newText("RESET", resetButton.x, resetButton.y, nil, 24)

Best of luck on your project.

Bob

[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

anil190201

[GLOBAL: userInfoPane.html]
anil190201
  • Observer

  • 1 posts
  • Corona SDK

I have completed my project finally. But I need inputs for android. How can I make a UI like attached picture? I need to get input of 4 numbers. Thanks in advance.

Attached Files



[TOPIC: post.html]
#2

ldurniat

[GLOBAL: userInfoPane.html]
ldurniat
  • Contributor

  • 389 posts
  • Corona SDK

[TOPIC: post.html]
#3

Blex

[GLOBAL: userInfoPane.html]
Blex
  • Corona Geek

  • 1,199 posts
  • Corona SDK

That's not very difficult, just create a table of each of the squares x and y values, and then use a for loop to create them.



[TOPIC: post.html]
#4

cyberparkstudios

[GLOBAL: userInfoPane.html]
cyberparkstudios
  • Contributor

  • 573 posts
  • Corona SDK

  Best Answer

Here is a sample code that might do what you want.  This is just one of many ways to do the same thing.  Probably better. more efficient ways, but this way works.  Hopefully it will be good to learn from.

local w = display.contentWidth
local h = display.contentHeight
local horzMargin = w * .10
local vertMargin = h * .20
local horzSpace = 1
local vertSpace = 1
local horzCnt = 5
local vertCnt = 4
local cellWidth = (w - (horzMargin * 2)) / horzCnt
local cellHeight = cellWidth

local grid = display.newGroup()
local posX = cellWidth * .5
local posY = cellHeight * .5
local horzCursor = 0
local cellCnt = 0

local inputCnt = 0
local inputTotal = 0

local resultText
local resetButton
local resetText

local function onCellTouch(e)
	if e.phase == "ended" then
		if inputCnt < 4 and e.target.alpha == 1 then
			e.target.alpha = .5
			inputTotal = inputTotal + e.target.id
			inputCnt = inputCnt + 1
			resultText.text = inputTotal
			if inputCnt == 4 then
				resultText:setFillColor(.7, .1, .1)
			end
		end
	end
end

local function resetGrid(e)	
	if e.phase == "began" then
		e.target.alpha = .5
	elseif e.phase == "ended" then
		for i = 1, grid.numChildren do
			grid[i].alpha = 1			
		end
		resultText:setFillColor(1)
		inputTotal = 0
		inputCnt = 0
		resultText.text = "---"
		e.target.alpha = 1
	end
end

for i = 1, vertCnt do
	horzCursor = horzCursor + 1	
 for ii = 1, horzCnt do
 	local cell, cellRect, cellText
 	cellCnt = cellCnt + 1
 	cell = display.newContainer(cellWidth, cellHeight)
 	cellRect = display.newRect(0, 0, cellWidth, cellHeight)
 	cellRect:setFillColor(.7, .1, .1)
 	cellText = display.newText(cellCnt, 0, 0, nil, 24)
 	cellText:setFillColor(0)
 	cell:insert(cellRect)
 	cell:insert(cellText)
 	grid:insert(cell)
 	cell.x = posX
 	cell.y = posY
 	cell:addEventListener("touch", onCellTouch)
 	cell.id = cellCnt
 	 	
 	posX = posX + cellWidth + horzSpace
 end
 	posX = cellWidth * .5
 	posY = posY + cellHeight + vertSpace
end

-- center the 'grid' on the screen
grid.x = (w * .5) - (grid.width * .5)
grid.y = (h * .5) - (grid.height * .5)

resultText = display.newText("---", w * .5, 30, nil, 32)
resetButton = display.newRect(w * .5, h - 30, w * .25, h * .1)
resetButton:setFillColor(.5, .5, .5)
resetButton:addEventListener("touch", resetGrid)
resetText = display.newText("RESET", resetButton.x, resetButton.y, nil, 24)

Best of luck on your project.

Bob




[topic_controls]
[/topic_controls]