gpt4 book ai didi

java - Android EditText - 小数点分隔符

转载 作者:行者123 更新时间:2023-11-30 10:50:43 27 4
gpt4 key购买 nike

我有两个 EditText。在第一个中,用户输入一个数字,可以是小数。在第二个 EditText 中,数字被显示,但被格式化,四舍五入。我使第二个 EditText 不可点击,因为用户不应该能够更改此值。由于样式原因,我更喜欢使用 EditText 而不是 TextView。问题在于带有逗号小数错误的外国数字。我的应用程序出现此错误 java.lang.NumberFormatException: Invalid double: "1,60934" 由使用逗号分隔符的人造成。

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

有什么解决办法吗?

最佳答案

根据这部分代码:

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 提示用户,但您可能认为它不会完全解决此问题,因为会自动更改此字符。

使用 StringDialogs 来验证这些字段。

希望对你有帮助

关于java - Android EditText - 小数点分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34969724/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com