我想对数组进行排序。我无法弄清楚如何从edittext字段获取输入并将单个数字转换为数组。
答案 0 :(得分:0)
先做好
String s = yourEditText.toString();
Integer digitNumber = s.length();
Integer i = Integer.valueOf(s);
然后ull必须读取单个数字你这样做是通过使用Mod运算符
new Integer[] I = new Integer[digitNumber];
for(int i=0; i<digitNumber;i++){
I[i] = i % 10;
i = i/10;
}
答案 1 :(得分:0)
从EditText获取String,然后使用for循环将每个数字存储到数组中。
public class EditTextArray extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_text_array);
EditText et = (EditText) findViewById(R.id.editText);
et.setText("1452"); // Normally user input here...
String text = et.getText().toString();
int[] array = new int[text.length()];
for (int i = 0; i < text.length(); i++) {
array[i] = Character.getNumericValue(text.charAt(i));
}
Log.d("Output: ", Arrays.toString(array));
//09-20 14:27:45.272 20400-20400/com.jtwaller.test D/Output:: [1, 4, 5, 2]
StringBuilder sb = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
sb.append(array[i] + " ");
}
Log.d("Output: ", sb);
//09-20 14:27:45.272 20400-20400/com.jtwaller.test D/Output:: 1 4 5 2
}
}
答案 2 :(得分:0)
就这样做...获取你的String并用parseInt转换它
String number = "10";
int result = Integer.parseInt(number);