今天写了安卓的部分模块代码
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;
}
}
标签:findViewById,日报,EditText,软工,private,5.17,import,id,TextView
From: https://www.cnblogs.com/drip3775/p/18198865