Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android提示、税务、总计算器_Java_Android - Fatal编程技术网

Java Android提示、税务、总计算器

Java Android提示、税务、总计算器,java,android,Java,Android,你好,我也是Java新手,正在学习如何构建android应用程序。 我有一个计算器,我一直在工作,我需要帮助找出如何设置值编辑文本。当用户在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 = findV

你好,我也是Java新手,正在学习如何构建android应用程序。 我有一个计算器,我一直在工作,我需要帮助找出如何设置值编辑文本。当用户在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 + "");

}

实现将是这样的

EditText editText = findViewById(R.id.editText);

if (editText.getText().toString().equals("CA")) {
    textView.setText("CA=7%");
}

您可以将这些百分比存储到静态变量,然后分配它们。如果我正确理解您的问题,可能的解决方案如下:

//outside of the calculate method

protected static String CA_PERCENTAGE = "CA = 7%";
protected static String OR_PERCENTAGE = "OR = 8%";
protected static String FL_PERCENTAGE = "FL = 10%";

//inside your calculate method
switch(area){

    case "CA" : 
        tipAmountTV.setText(CA_PERCENTAGE);
    case "OR" :
        tipAmountTV.setText(OR_PERCENTAGE);
    case "FL" :
        tipAmountTV.setText(FL_PERCENTAGE);
}
当然,这不是最漂亮的事情,但因为您是Java新手,所以可以使用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);