gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:59:30 25 4
gpt4 key购买 nike

我想做一个小应用程序。
这个应用程序应该做的是拥有一个正在运行的“银行帐户”总额,并且有两个选项:存款或取款。
我附上了主要的 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/

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