[英]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。
玩String
和Dialogs
来验证这些字段。
希望对你有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.