Java 如何以正确的方式从 EditText 获得双倍?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23539713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:44:09 来源:igfitidea点击:
How to get double from EditText the right way?
提问by user3460769
Does anyone know how to parse a double from an EditText?
有谁知道如何从 EditText 解析双精度值?
I know how to do it, but it isn't the right way I think. Does anyone have best practises?
我知道该怎么做,但这不是我认为的正确方式。有没有人有最佳实践?
Thank you.
谢谢你。
采纳答案by Ersin Gülbahar
you can try this:
你可以试试这个:
double value;
String text =your_edittext.getText().toString();
if(!text.isEmpty())
try
{
value= Double.parseDouble(text);
// it means it is double
} catch (Exception e1) {
// this means it is not double
e1.printStackTrace();
}
回答by Aniruddha
Whatever you type in EditText is String.
无论您在 EditText 中键入什么,都是字符串。
How to check if edittext is empty?
如何检查edittext是否为空?
String str = edit_text.getText().toString();
if (str.trim().equals("")
Toast.makeText(getApplicationContext(),
"EditText is empty.",
Toast.LENGTH_SHORT).show();