今天寫了安卓的部分模組程式碼
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class ExpenseReportActivity extends AppCompatActivity {
private TextView textViewName;
private TextView textViewPosition;
private TextView textViewAge;
private TextView textViewReason;
private TextView textViewDates;
private EditText editTextStartDate;
private EditText editTextStartPoint;
private EditText editTextEndDate;
private EditText editTextEndPoint;
private EditText editTextFoodAllowance;
private TextView textViewTotalAmount;
private Button submitButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expense_report);
// 初始化檢視
textViewName = findViewById(R.id.textView_name);
textViewPosition = findViewById(R.id.textView_position);
textViewAge = findViewById(R.id.textView_age);
textViewReason = findViewById(R.id.textView_reason);
textViewDates = findViewById(R.id.textView_dates);
editTextStartDate = findViewById(R.id.editText_start_date);
editTextStartPoint = findViewById(R.id.editText_start_point);
editTextEndDate = findViewById(R.id.editText_end_date);
editTextEndPoint = findViewById(R.id.editText_end_point);
editTextFoodAllowance = findViewById(R.id.editText_food_allowance);
textViewTotalAmount = findViewById(R.id.textView_total_amount);
submitButton = findViewById(R.id.submit_button);
// 假設資料透過intent傳遞或從資料庫中獲取
String name = "張三";
String position = "經理";
String age = "35";
String reason = "出差";
String dates = "2024-05-01 至 2024-05-10";
// 回顯資料
textViewName.setText(name);
textViewPosition.setText(position);
textViewAge.setText(age);
textViewReason.setText(reason);
textViewDates.setText(dates);
// 新增TextWatchers以便在值變化時計算總金額
TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
calculateTotalAmount();
}
};
editTextFoodAllowance.addTextChangedListener(textWatcher);
// 提交按鈕點選事件
submitButton.setOnClickListener(v -> {
// 這裡可以新增提交邏輯,例如儲存到資料庫
Toast.makeText(ExpenseReportActivity.this, "報銷單提交成功", Toast.LENGTH_SHORT).show();
});
}
private void calculateTotalAmount() {
double foodAllowance = getDoubleFromEditText(editTextFoodAllowance);
// 其他費用如果有的話可以在這裡獲取並相加
double totalAmount = foodAllowance;
textViewTotalAmount.setText(String.valueOf(totalAmount));
}
private double getDoubleFromEditText(EditText editText) {
String text = editText.getText().toString();
if (!text.isEmpty()) {
try {
return Double.parseDouble(text);
} catch (NumberFormatException e) {
return 0;
}
}
return 0;
}
}