- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个 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
提示用户,但您可能认为它不会完全解决此问题,因为会自动更改此字符。
使用 String
和 Dialogs
来验证这些字段。
希望对你有帮助
关于java - Android EditText - 小数点分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34969724/
/** Called when the user clicks the Send button */ public void sendMessage(View view) { Intent i
我已经编写了 EditText 的子类。这是那个子类: package com.package.foo; import android.content.Context; import android.
我有一个用户名和密码 XML 段 我希望当用户名字段到达第9个字符时光标焦点自动跳转到密码字段 我在看 Focus next textview automatically并且正在尝试: fin
我知道这类问题被问过很多次。但仍然没有人给出完美的答案。 我有问题: 我想从 EditText1 ** 移动到另一个 **EditText2 。我已经检测到editText1,但如何将光标移动到edi
我有 2 个 EditText(myEditText1 和 myEditText2) 我需要触摸 myEditText1 1.做一些生意[这里没有问题,需要申请的业务] 2. 然后请求焦点到 myEd
我想在用户输入 EditText 时将文本放入我的应用程序中的某个字符串中,并使用它在 Activity 中生动地显示它(在不同的 View 中......) - 就像谷歌的实时/即时搜索一样...
当我将文本插入到我的 EditText 字段时,文本本身与 EditText 的行之间存在异常间隙。这是我终端的打印屏幕,您可以在其中看到我正在谈论的这个差距,它被标记为红色。 我试过文本对齐和引力但
我的应用程序需要帮助。这是我的相对布局的 xml 文件,我需要在按下 Enter 键后跳转到下一个编辑文本。但是有一个问题,如果我正在写入 editTextKm 并按 Enter 键,它会跳转到 ed
Hello All 我已经将 onLongClickListener() 用于我的 EditText View ,但是每当我长时间单击 View 时,就会出现一个弹出窗口,不允许我执行我的操作onLo
EditText 快把我们逼疯了!真的! 我们的应用程序登录屏幕出现问题。它包含两个 EditText,但只有第一个(用户名)获得焦点,通过 SoftKeyboard 仅在触摸/单击用户名 EditT
如何在kotlin中获取editText并用toast显示。 var editTextHello = findViewById(R.id.editTextHello) 我试过了,但显示对象 Toast
我一直在浏览各种线索来解决这个问题,但我仍然感到困惑。当我将“adjustPan”应用于底部具有 EditText 的 Activity 时,UI 会正确向上推,但编辑文本会被底部的键盘稍微遮挡。经过
我有两个类:a 和 b。在类 a 中,我有一个线程,当变量 x 的值小于 1000 时,它会将变量 x 加一。在类 b(Activity 类)中,我有一个名为 ed 的 EditText。每当a类中x
今天我遇到了一个非常有趣的情况。 假设: EditText 从 XML 实例化。 EditText editText; editText = findViewByID(R.id.editT
有谁知道我的代码出了什么问题,我正在尝试比较编辑文本字段中的输入,以确保它们在创建帐户之前具有相同的值。 //compare the fields contents if(editT
制作一个简单的卡路里转换器。 这是图片中的问题。 我使用的是 Genymotion 模拟器。 这是我遇到问题的屏幕部分: 当我单击 EditText 数字字段时,数字键盘会出现 但是我必须单击下一个箭
全部 - 我试图隐藏 EditText B、C,直到 EditText A 中至少有一个字符。我尝试过使用文本观察器... EditText editText = (EditText) findVie
我正在努力完成 “Sam 的 24 小时 Android 应用程序开发技术”,但在某些时候我的昵称和电子邮件设置停止正确保存。它现在根本不保存昵称并将电子邮件保存到两者。我做了什么导致这个,我该如何解
我有一个 SharedPreferences,它保存来自一个 Activity 的 EditText 输入并在另一个 Activity 中显示字符串值。 当我在 EditText 字段中输入内容并按下
这一定是一个我可能会再次忽略的简单问题。这是我的问题我的 string.xml 上有一个字符串数组 Alabama Florida Los Angeles Virgi
我是一名优秀的程序员,十分优秀!