2017-05-27 1 views
0

問題は、txt.textが空の場合です。エラーが発生します。luaのTextFiled Inputが空の場合のエラー?

--input text 
local txt = native.newTextField(160,100,300,50) 
--button press to get answer 
local btn = display.newRect(160,300,120,40) 
--label text to show answer 
local label = display.newText("answer",160,200) 

txt.inputType = "number" 

function doit(e) 
    -- Currency exchange (USD to any country) 
    label.text = txt.text * 2 
    -- when txt.text == empty I get error 
end 

btn:addEventListener("tap",doit) 

私はif elseを使用しようとしましたが、同じ問題です。

答えて

1

適切なifステートメントを追加するだけです。お試しください:

function doit (e) 
    if tonumber(txt.text) then 
     label.text = tonumber(txt.text) * 2 
    end 
end 

残りのコードは同じです。

関連する問題