简体   繁体   English

如何从一个EditText获取多个变量?

[英]How to get multiple variables from one EditText?

I'm looking for a way to get three float values from one EditText.I've Looked of the developers page but i couldnt find anything that best fit my need.I would like to get three floats ax,y,z from one entry of EditText? 我正在寻找一种方法来从一个EditText中获取三个float值。我已经查看了开发人员页面,但是找不到最适合我需要的东西。我想从一个条目中获取三个float ax,y,z的EditText?

Thank you guys so much on the replys o this Question im really greatfull. 非常感谢你们对这个问题的答复。 Im limited to the time on my computer right now but ill let you know which answer work for me and ill post my code too. 我现在的时间仅限于我在计算机上的时间,但生病的人会告诉您哪个答案对我有用,我也会生我的代码。

If they are separated by whitespaces 如果它们之间用空格隔开

Scanner sc = new Scanner(yourEditText.getText().toString());

float vals = new float[3];

while(int i=0; sc.hasNextFloat() && (i < 3); ++i) {
   vals[i] = sc.nextFloat();
}

otherwise use Scanner.setDelimiter to set delimiter(s) other than whitespaces. 否则,请使用Scanner.setDelimiter设置除空格以外的分隔符。

Reconsider your design to use three EditTexts instead. 重新考虑您的设计以改为使用三个EditText。 On mobile phones it's a pain to enter symbols like comma 在手机上输入逗号等符号很麻烦

String value = yourEditText.getText().toString();
String[] floatStrings = value.split(",");
float[] result = new float[floatString.length];
for (int i=0; i<result.length; i++) {
    result[i] = Float.valueOf(floatStrings[i];
}

?

如果希望用户输入3个单独的值,则将使用3个EditText小部件,这些值将被解析为3个单独的变量。

You should either create several EditText views, or then parse the contents of the EditText 您应该创建几个EditText视图,或者然后解析EditText的内容

EditText textView;

String[] splitFloatStrings = textView.getText().toString();.split(",");
float[] parsedFloats = new float[splitFloatStrings.length];

for (int i=0; i < parsedFloats.length; i++) {
    parsedFloats[i] = Float.valueOf(splitFloatStrings[i];
}

As per Java parsing a string of floats to a float array? 按照Java将一串float解析为float数组?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM