帮我用Android Studio 写一个贷款计算器的程序

时间: 2023-11-22 14:48:28 浏览: 42
以下是一个简单的房贷计算器的Android Studio程序,其中包括CheckBox图标的改变、EditText格式的改变、Spinner的使用和房贷的计算逻辑。你可以根据自己的需要进行修改和扩展。 ```java import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Spinner; import android.widget.TextView; import java.text.DecimalFormat; public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener { private EditText etLoanAmount, etLoanTerm, etInterestRate; private TextView tvMonthlyPayment, tvTotalPayment, tvTotalInterest; private CheckBox cbTaxes, cbInsurance; private Spinner spLoanType; private Button btnCalculate; private double loanAmount, interestRate, monthlyPayment, totalPayment, totalInterest; private int loanTerm, loanType; private boolean taxes, insurance; private DecimalFormat df = new DecimalFormat("#.##"); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etLoanAmount = findViewById(R.id.et_loan_amount); etLoanTerm = findViewById(R.id.et_loan_term); etInterestRate = findViewById(R.id.et_interest_rate); tvMonthlyPayment = findViewById(R.id.tv_monthly_payment); tvTotalPayment = findViewById(R.id.tv_total_payment); tvTotalInterest = findViewById(R.id.tv_total_interest); cbTaxes = findViewById(R.id.cb_taxes); cbInsurance = findViewById(R.id.cb_insurance); spLoanType = findViewById(R.id.sp_loan_type); btnCalculate = findViewById(R.id.btn_calculate); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.loan_types, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spLoanType.setAdapter(adapter); spLoanType.setOnItemSelectedListener(this); etLoanAmount.addTextChangedListener(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) { if (!s.toString().equals("")) { loanAmount = Double.parseDouble(s.toString()); } else { loanAmount = 0; } } @Override public void afterTextChanged(Editable s) {} }); etLoanTerm.addTextChangedListener(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) { if (!s.toString().equals("")) { loanTerm = Integer.parseInt(s.toString()); } else { loanTerm = 0; } } @Override public void afterTextChanged(Editable s) {} }); etInterestRate.addTextChangedListener(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) { if (!s.toString().equals("")) { interestRate = Double.parseDouble(s.toString()); } else { interestRate = 0; } } @Override public void afterTextChanged(Editable s) {} }); cbTaxes.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { taxes = cbTaxes.isChecked(); } }); cbInsurance.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { insurance = cbInsurance.isChecked(); } }); btnCalculate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { switch (loanType) { case 0: calculateEqualPrincipal(); break; case 1: calculateEqualInstallment(); break; case 2: calculateReverseEqualPrincipal(); break; case 3: calculateReverseEqualInstallment(); break; } tvMonthlyPayment.setText(df.format(monthlyPayment)); tvTotalPayment.setText(df.format(totalPayment)); tvTotalInterest.setText(df.format(totalInterest)); } }); } private void calculateEqualPrincipal() { double monthlyInterestRate = interestRate / 12; double principal = loanAmount / loanTerm; double interest = loanAmount * monthlyInterestRate; monthlyPayment = principal + interest; totalPayment = monthlyPayment * loanTerm; totalInterest = totalPayment - loanAmount; if (taxes) { totalPayment += loanTerm * 100; } if (insurance) { totalPayment += loanTerm * 50; } } private void calculateEqualInstallment() { double monthlyInterestRate = interestRate / 12; double monthlyPaymentRate = monthlyInterestRate * Math.pow(1 + monthlyInterestRate, loanTerm) / (Math.pow(1 + monthlyInterestRate, loanTerm) - 1); monthlyPayment = loanAmount * monthlyPaymentRate; totalPayment = monthlyPayment * loanTerm; totalInterest = totalPayment - loanAmount; if (taxes) { totalPayment += loanTerm * 100; } if (insurance) { totalPayment += loanTerm * 50; } } private void calculateReverseEqualPrincipal() { double monthlyInterestRate = interestRate / 12; double principal = loanAmount / loanTerm; double interest = loanAmount * monthlyInterestRate; double monthlyPayment1 = principal + interest; double monthlyPayment2 = monthlyPayment1 + principal * monthlyInterestRate; double monthlyPayment3 = monthlyPayment2 + principal * monthlyInterestRate; double monthlyPayment4 = monthlyPayment3 + principal * monthlyInterestRate; double monthlyPayment5 = monthlyPayment4 + principal * monthlyInterestRate; double monthlyPayment6 = monthlyPayment5 + principal * monthlyInterestRate; double monthlyPayment7 = monthlyPayment6 + principal * monthlyInterestRate; double monthlyPayment8 = monthlyPayment7 + principal * monthlyInterestRate; double monthlyPayment9 = monthlyPayment8 + principal * monthlyInterestRate; double monthlyPayment10 = monthlyPayment9 + principal * monthlyInterestRate; double monthlyPayment11 = monthlyPayment10 + principal * monthlyInterestRate; double monthlyPayment12 = monthlyPayment11 + principal * monthlyInterestRate; monthlyPayment = (monthlyPayment1 + monthlyPayment2 + monthlyPayment3 + monthlyPayment4 + monthlyPayment5 + monthlyPayment6 + monthlyPayment7 + monthlyPayment8 + monthlyPayment9 + monthlyPayment10 + monthlyPayment11 + monthlyPayment12) / 12; totalPayment = monthlyPayment * loanTerm; totalInterest = totalPayment - loanAmount; if (taxes) { totalPayment += loanTerm * 100; } if (insurance) { totalPayment += loanTerm * 50; } } private void calculateReverseEqualInstallment() { double monthlyInterestRate = interestRate / 12; double monthlyPaymentRate = monthlyInterestRate * Math.pow(1 + monthlyInterestRate, loanTerm) / (Math.pow(1 + monthlyInterestRate, loanTerm) - 1); double monthlyPayment1 = loanAmount * monthlyPaymentRate; double monthlyPayment2 = (loanAmount - monthlyPayment1) * monthlyInterestRate + monthlyPayment1; double monthlyPayment3 = (loanAmount - monthlyPayment1 - monthlyPayment2) * monthlyInterestRate + monthlyPayment2; double monthlyPayment4 = (loanAmount - monthlyPayment1 - monthlyPayment2 - monthlyPayment3) * monthlyInterestRate + monthlyPayment3; double monthlyPayment5 = (loanAmount - monthlyPayment1 - monthlyPayment2 - monthlyPayment3 - monthlyPayment4) * monthlyInterestRate + monthlyPayment4; double monthlyPayment6 = (loanAmount - monthlyPayment1 - monthlyPayment2 - monthlyPayment3 - monthlyPayment4 - monthlyPayment5) * monthlyInterestRate + monthlyPayment5; double monthlyPayment7 = (loanAmount - monthlyPayment1 - monthlyPayment2 - monthlyPayment3 - monthlyPayment4 - monthlyPayment5 - monthlyPayment6) * monthlyInterestRate + monthlyPayment6; double monthlyPayment8 = (loanAmount - monthlyPayment1 - monthlyPayment2 - monthlyPayment3 - monthlyPayment4 - monthlyPayment5 - monthlyPayment6 - monthlyPayment7) * monthlyInterestRate + monthlyPayment7; double monthlyPayment9 = (loanAmount - monthlyPayment1 - monthlyPayment2 - monthlyPayment3 - monthlyPayment4 - monthlyPayment5 - monthlyPayment6 - monthlyPayment7 - monthlyPayment8) * monthlyInterestRate + monthlyPayment8; double monthlyPayment10 = (loanAmount - monthlyPayment1 - monthlyPayment2 - monthlyPayment3 - monthlyPayment4 - monthlyPayment5 - monthlyPayment6 - monthlyPayment7 - monthlyPayment8 - monthlyPayment9) * monthlyInterestRate + monthlyPayment9; double monthlyPayment11 = (loanAmount - monthlyPayment1 - monthlyPayment2 - monthlyPayment3 - monthlyPayment4 - monthlyPayment5 - monthlyPayment6 - monthlyPayment7 - monthlyPayment8 - monthlyPayment9 - monthlyPayment10) * monthlyInterestRate + monthlyPayment10; double monthlyPayment12 = (loanAmount - monthlyPayment1 - monthlyPayment2 - monthlyPayment3 - monthlyPayment4 - monthlyPayment5 - monthlyPayment6 - monthlyPayment7 - monthlyPayment8 - monthlyPayment9 - monthlyPayment10 - monthlyPayment11) * monthlyInterestRate + monthlyPayment11; monthlyPayment = (monthlyPayment1 + monthlyPayment2 + monthlyPayment3 + monthlyPayment4 + monthlyPayment5 + monthlyPayment6 + monthlyPayment7 + monthlyPayment8 + monthlyPayment9 + monthlyPayment10 + monthlyPayment11 + monthlyPayment12) / 12; totalPayment = monthlyPayment * loanTerm; totalInterest = totalPayment - loanAmount; if (taxes) { totalPayment += loanTerm * 100; } if (insurance) { totalPayment += loanTerm * 50; } } @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { loanType = position; } @Override public void onNothingSelected(AdapterView<?> parent) {} } ```

相关推荐

最新推荐

recommend-type

Android Studio实现简易计算器(表格布局TableLayout)

主要为大家详细介绍了Android Studio实现简易计算器,表格布局TableLayout,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

android studio实现简单考试应用程序实例代码详解

主要介绍了android studio实现简单考试应用程序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

Android studio创建第一个app

主要为大家详细介绍了如何使用Android studio创建你的第一个项目Hello World,感兴趣的小伙伴们可以参考一下
recommend-type

基于三层感知机实现手写数字识别-内含源码和说明书.zip

基于三层感知机实现手写数字识别-内含源码和说明书.zip
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依