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

标签 java android

你好,我也是 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/

相关文章:

java - DataSnapshot.getValue() 未正确检索对象

java - 不加return语句避免DD异常

android - 替换 fragment 然后快速按回导致UI延迟响应,阴影和角落效果消失

java - 使用什么工具包可以在绘画中制作多个线程? Swing、GWT、FX、Qt 还是什么?

java - 确定任务是否较早,同时只知道日期是否较早以及时间是否较早

Java Scanner 未接受有效输入或与整数解析相关的其他问题

java - Spring 批处理 : How to set up a job repository for every job

java - 如何修复 Android 错误“类 Context 中的方法 openFileInput 无法应用于给定类型;2

android - Mediaplayer 在 reset() 和 prepareAsync() 调用时抛出 Error(-38,0)

android - 如何修复黑莓应用程序提交兼容性?