java - 将 EditText 中的字符串转换为 Double

标签 java android android-edittext

我想做一个小应用程序。
这个应用程序应该做的是拥有一个正在运行的“银行帐户”总额,并且有两个选项:存款或取款。
我附上了主要的 Activity 类以及我为“银行帐户”制作的类(我还没有实现历史功能,因为我还没有弄清楚这部分!)。
基本上,这一行:

bankAccount.withdrawal(Double.parseDouble(findViewById(R.id.inputWithdrawal).toString()))

它的存款对应项抛出 NumberFormatException 说它是“无效的 double ”。
我不知道我在其他线程上看到了什么,但我还没有找到任何有用的东西。

import android.support.v7.app.AppCompatActivity;   
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    private EditText withdrawal, deposit;
    private Button withdrawalButton, depositButton;
    private BankAccount bankAccount;
    private String total;
    private TextView textViewTotal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        withdrawal = (EditText) findViewById(R.id.inputWithdrawal);
        deposit = (EditText) findViewById(R.id.inputDeposit);

        withdrawalButton = (Button) findViewById(R.id.withdrawalButton);
        depositButton = (Button) findViewById(R.id.depositButton);
        withdrawalButton.setOnClickListener(onClickListener);
        depositButton.setOnClickListener(onClickListener);

        bankAccount = new BankAccount();
        textViewTotal = (TextView) findViewById(R.id.Total);
        updateTotal();
    }

    private View.OnClickListener onClickListener = new View.OnClickListener(){
        @Override
        public void onClick(View view){
            try {
                switch (view.getId()) {
                    case R.id.withdrawalButton:
                        if (!withdrawal.getText().toString().equals("")) {
                            bankAccount.withdrawal(Double.parseDouble(findViewById(R.id.inputWithdrawal).toString()));
                            updateTotal();
                        }
                        break;

                    case R.id.depositButton:
                        if (!deposit.getText().toString().equals("")) {
                            bankAccount.deposit(Double.parseDouble(findViewById(R.id.inputDeposit).toString()));
                            updateTotal();
                        }
                        break;
                }
            } catch (NumberFormatException e){
                e.printStackTrace();
            }
        }};

    public void updateTotal(){
        total = "$" + bankAccount.getCheckingTotal();
        textViewTotal.setText(total);
    }  
}

public class BankAccount {
    private double checkingTotal;
    private ArrayList<String> history;

    public BankAccount(){
        checkingTotal = 0;
        history = new ArrayList<String>();
    }

    public void withdrawal(double amount){
        checkingTotal -= amount;
        history.add("-$" + amount);

        if(history.size() > 5)
            history.remove(0);
    }

    public void deposit(double amount){
        checkingTotal += amount;
        history.add("$" + amount);

        if(history.size() > 5)
            history.remove(0);
    }

    public double getCheckingTotal(){
        return checkingTotal;
    }
}

最佳答案

findViewById(R.id.inputWithdrawal) 返回一个 View

View.toString() 为您提供一些垃圾值(不是 EditText 的内容)。

已经拥有 withdrawal = (EditText) findViewById(R.id.inputWithdrawal);,因此使用它来获取字符串(就像您已经做的那样getText().toString() 来查看字符串是否为空)。

String w = withdrawal.getText().toString();
if (!TextUtils.isEmpty(w)) {
    bankAccount.withdrawal(Double.parseDouble(w));
    updateTotal();
}

提示:始终避免额外的 findViewById 调用。

关于java - 将 EditText 中的字符串转换为 Double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42171580/

相关文章:

java - jmeter 无法运行!无法找到 Java 可执行文件或版本。请检查您的 Java 安装”

java - 将 String 设置为不为 null 的值

java - 在 Firebase 和 Retrieve 中保存数据

android - 在禁用的 EditText 上启用复制并解释 android editText 的奇怪行为

java - jax-ws ri 2.2.1 没有为 amazon ecs wsdl 文件生成枚举

java - Postgresql JDBC 驱动程序是否支持 Pgpass 身份验证?

android - Android 上的 OpenGL 漫射照明

android - 整个android应用程序的触摸事件

java - Android 不支持换行 (\n)?

android - inputType EditText中的textMultiLine和textImeMultiLine有什么区别?