gpt4 book ai didi

java - Android 小费、税金、总计计算器

转载 作者:行者123 更新时间:2023-12-02 11:26:33 25 4
gpt4 key购买 nike

你好,我也是 Java 新手,正在学习如何构建 Android 应用程序。我有一个我一直在研究的计算器,我需要帮助来弄清楚如何为 EditText 设置值。当用户在 EditText 中输入 CA、OR 或 FL 时,我将如何为其分配值? CA = 7%,OR = 8%,FL = 10% 谢谢

public void calculate(View view) {

EditText billET = findViewById(R.id.edit_bill_amount);
EditText tipPercentET = findViewById(R.id.edit_tip_percent);
EditText taxPercentET = findViewById(R.id.edit_tax_percent);


String billString = billET.getText().toString();
String tipPercentString = tipPercentET.getText().toString();
String taxPercentString = taxPercentET.getText().toString();



float bill = Float.parseFloat(billString);
int tipPercent = Integer.parseInt(tipPercentString);
int taxPercent = Integer.parseInt(taxPercentString);

TipCalculator tc = new TipCalculator(tipPercent / 100.0F, bill, taxPercent / 100.0F);
float taxAmount = tc.taxAmount();
float tipAmount = tc.tipAmount();
float total = tc.totalAmount();

TextView taxAmountTV = findViewById(R.id.label_tax_amount_value);

TextView tipAmountTV = findViewById(R.id.label_tip_amount_value);
TextView totalAmountTV = findViewById(R.id.label_total_amount_value);

taxAmountTV.setText(taxAmount + "");
tipAmountTV.setText(tipAmount + "");
totalAmountTV.setText(total + "");

}

最佳答案

您可以使用TextWatcher来实现它。

TextWatcher watcher;
watcher=new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) { }

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

@Override
public void afterTextChanged(Editable s) {
edittext.removeTextChangedListener(watcher);
switch(s.toString){

case "CA" :
edittext.setText("7%");
case "OR" :
edittext.setText("8%");
case "FL" :
edittext.setText("10%");
}
edittext.addTextChangedListener(watcher);
}
};
edittext.addTextChangedListener(watcher);

关于java - Android 小费、税金、总计计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49586765/

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