Y.K Y.K - 3 months ago 19
Java Question

Android: How to make EditText digital input string into int?

I want to enter a figure which can be sent out with
By:

usbService.write ()
;

C1
is the effect I want, but would like to form
C2
by
EditText
achieve the same effect

Because the need to use
byte []
, so it is necessary do the conversion action
Current input (128), a single number can reach my desired effect.

But I need a continuous instruction such as: (128 135)
int i = Integer.parseInt (data);
will collapse.

By
EditText
want to enter a series of numbers into the instruction
byte []
into
Achieve the same effect the following

byte p1 = (byte) 128
byte p2 = (byte) 135

byte [] c1 = new byte [128];
c1 [0] = p1;
c1 [1] = p2;
usbService.write (c1);

@Override
public void onClick(View v) {

String data = editText.getText().toString();

int i=Integer.parseInt(data);
byte [] C2= ByteBuffer.allocate(1024).putInt(i).array();


display.append(data);
usbService.write(C2);
}

Answer

Set the input type of your editText to number something like below:

<EditText
    android:id="@+id/edit_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number|text"/>

And while fetching the numbers you can fetch them and split them using space something like below:

String data = editText.getText().toString();
String[] numberStrings = data.split(" ");
int[] numberArray = new int[numberStrings.length];
for (int i = 0; i < numberStrings.length; i++){
    numberArray[i] = Integer.parseInt(numberStrings[i].trim());
}

this way you will have all the numbers in that numberArray. Hope it helps.