gpt4 book ai didi

java - 无法使用 EditText Android 添加小数(如 0.25)

转载 作者:行者123 更新时间:2023-12-02 08:58:55 25 4
gpt4 key购买 nike

我有一个带有 editText 的应用程序,我想插入从(0.25-0.50-0.75 等)开始的小数,因为我想用其他 editText 数字,到目前为止我只能插入数字,如 1-2-3...请问有什么想法吗?

MyCode

price_per_piece.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

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

@SuppressLint("DefaultLocale")
@Override
public void afterTextChanged(Editable s) {
if (!s.toString().equals("")) {
//case_qty = v.findViewById(R.id.et_add_item_case_quanitiy); this line for understanding
//Note : case_qty is EditText which i want to insert decimal numbers
String qty_txt = case_qty.getText().toString();
if (qty_txt.equals("")){
qty_txt = "1";
// qty_txt = "0.25";
}
float qty = Float.parseFloat(qty_txt);
float p = Float.valueOf(s.toString());
price_per_case.setText(String.valueOf(qty * p));
}
}
});

提前致谢

最佳答案

android:inputType="numberDecimal" 添加到您的 EditText xml 声明中(如果您尚未这样做)。然后尝试使用 Double.parseDouble() 而不是 float

关于java - 无法使用 EditText Android 添加小数(如 0.25),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60316604/

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