I am new to Java, I need to know how do I format android EditText after pasting a number to it?
I have following XML in my XML file to EditText:
<EditText
android:id="@+id/user_number_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="@dimen/_12sdp"
android:layout_marginBottom="@dimen/_12sdp"
android:background="@color/white"
android:inputType="number"
android:maxLength="14"
android:paddingTop="@dimen/_2sdp"
android:text="Your Number: "
android:textSize="@dimen/_15sp" />
For instance, after I paste "23989723412" to EditText, it should format it automatically to "129.897.234-12".
I did some research, however, I am not sure what event triggers when a number is pasted in EditText, but it may have to do something with afterTextChanged
but I don't know how to do it for this purpose in Java?
Thank you in advance.
If you want to get a dot every 3 numbers for improved reading.
ex 1290030022 = 1.290.030.022
Feel free to use the code snippet below in your Activity.java
:
EditText editText = findViewById(R.id.myeditText);
try {
DecimalFormat decimalFormat = new DecimalFormat();
Integer value = Integer.parseInt(editText.getText().toString());
String format = decimalFormat.format(value);
System.out.println("Result:" + format);
}catch (Exception e){
e.printStackTrace();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.