簡體   English   中英

Android EditText-小數點分隔符

[英]Android EditText - decimal separator

我有兩個EditText。 在第一個中,用戶輸入一個數字,可以是十進制。 在第二個EditText中,該數字將顯示但已格式化並四舍五入。 我使第二個EditText不可單擊,因為用戶不能更改該值。 由於樣式,我更喜歡使用EditText而不是TextView。 問題是帶有逗號十進制錯誤的外國數字。 我的應用程序收到此錯誤java.lang.NumberFormatException: Invalid double: "1,60934"使用逗號分隔符的人出現java.lang.NumberFormatException: Invalid double: "1,60934"

XML文件

<?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文件

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) {
            }
        });
    }

任何想法如何解決?

根據這部分代碼:

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));
                }
            }

您已經注意到,如果有逗號分隔符,Java會引發異常。

為了解決這個問題,添加如果給定的字符串帶有逗號,我的意思是寫這樣的代碼

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.\
}

如果可以檢測到逗號,您也可以使用AlertDialog提示用戶,但是您可能會認為它不能完全解決此問題,因為會自動更改此char。

StringDialogs來驗證這些字段。

希望對你有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM