将EditText中的String转换为Double

时间:2017-02-11 02:38:16

标签: java android android-edittext

我想做一个小应用程序。
这个应用程序应该做的是有一个正在运行的“银行账户”,有两种选择:存钱或取钱 我附上了主要活动类以及我为“银行账户”制作的课程(我还没有实现历史功能,因为我还没有弄清楚这部分!)。
基本上,这一行:

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

而且它的存款对手正在抛出一个 NumberFormatException ,说它是“无效的双重”。
我不知道我在其他线程上看到了什么但是ihavent能够找到任何有用的东西。

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

1 个答案:

答案 0 :(得分:4)

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来电。