简体   繁体   中英

Android EditText - decimal separator

I have two EditTexts. In the first one, the user inputs a number, can be a decimal. In the second EditText, the number is displayed, but formatted, rounded. I made the second EditText not clickable since the user shouldn't be able to change this value. I prefer to use an EditText instead of a TextView because of the style. The problem is with foreign numbers with a comma decimal error. My app is getting this error java.lang.NumberFormatException: Invalid double: "1,60934" by people who use a comma separator.

XML file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main" tools:context=".MainActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:gravity="center"
        android:id="@+id/value1"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/value2"
        android:layout_marginTop="100dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:clickable="false"
        android:cursorVisible="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="center"/>
</RelativeLayout>

Java file

public class MainActivity extends AppCompatActivity {

    public static EditText value1;
    public static EditText value2;
    public static double value1var;
    public static double value2var;
    public static DecimalFormat df;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        df = new DecimalFormat("0.#####", new DecimalFormatSymbols(Locale.FRANCE));

        value1 = (EditText)findViewById(R.id.value1);
        value2 = (EditText)findViewById(R.id.value2);

        value1.setText(df.format(1.0));
        value2.setText(df.format(1000));

        value1var = Double.parseDouble(value1.getText().toString());
        value2var = Double.parseDouble(value2.getText().toString());

        value1.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                String value = s.toString();
                double valuevar = Double.parseDouble(s.toString());

                if(value.length() > 0){
                    value2.setText(df.format(valuevar));
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(final CharSequence s, int start, int before, int count) {
            }
        });
    }

Any ideas how to fix it?

According to this part of code:

value1.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                String value = s.toString();
                double valuevar = Double.parseDouble(s.toString());

                if(value.length() > 0){
                    value2.setText(df.format(valuevar));
                }
            }

As you noticed, Java throws an exception if there's a comma separator.

To fix this problem, add if given string has comma, I mean write code like this

if (value1.contains(",")) {
//Here implement `for` loop, which would check which char of string has this problematic sign.

//Use string.charAt(i) function to compare

// use string.replace(int begin, int end, CharSequence cs) function to change it to point instead of comma.\
}

You can also prompt a user with AlertDialog if comma char would be detected, but as you may think it won't fix this problem entirely as changing automatically this char.

Play with String and Dialogs to make a validation of these fields.

Hope it help

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.

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