Android实现房贷计算器功能
本文实例为大家分享了android实现房贷计算器的具体代码,供大家参考,具体内容如下
package com.atomic.moretool;
import android.os.bundle;
import android.view.view;
import android.widget.button;
import android.widget.edittext;
import android.widget.listview;
import android.widget.simpleadapter;
import android.widget.textview;
import android.widget.toast;
import androidx.appcompat.app.appcompatactivity;
import java.math.bigdecimal;
import java.util.arraylist;
import java.util.hashmap;
import java.util.list;
import java.util.map;
public class mortgagecal extends appcompatactivity {
private edittext allloan,yearinterestrate,loanyear;
private button calloan;
private listview showdebx,showdebj;
private textview debxtotalinterest;
private textview debjtotalinterest;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_mortgagecal);
findcompent();
calloan.setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view view) {
showdebx();
showdebj();
}
});
}
private void showdebx(){
simpleadapter simpleadapter=new simpleadapter(this,cal_debx(),r.layout.show_debx,
new string[]{"debxmonth","debxmonthloan","debxmonthprincipal","debxmonthinterest"},
new int[]{r.id.debx_month,r.id.listview_debx_month_loan,r.id.listview_debx_month_principal,r.id.listview_debx_month_interest});
showdebx.setadapter(simpleadapter);
}
private void showdebj(){
simpleadapter simpleadapter=new simpleadapter(this,cal_debj(),r.layout.show_debj,
new string[]{"debjmonth","debjmonthloan","debjmonthprincipal","debjmonthinterest","debjmonthdecrease"},
new int[]{r.id.debj_month,r.id.listview_debj_month_loan,r.id.listview_debj_month_principal,r.id.listview_debj_month_interest,r.id.listview_debj_month_decrease});
showdebj.setadapter(simpleadapter);
}
private void findcompent() {
allloan=findviewbyid(r.id.all_loan);
yearinterestrate=findviewbyid(r.id.year_interest_rate);
loanyear=findviewbyid(r.id.loan_year);
allloan.setselectallonfocus(true);
yearinterestrate.setselectallonfocus(true);
loanyear.setselectallonfocus(true);
calloan=findviewbyid(r.id.cal_loan);
showdebx=findviewbyid(r.id.show_debx);
showdebj=findviewbyid(r.id.show_debj);
debxtotalinterest=findviewbyid(r.id.debx_total_interest);
debjtotalinterest=findviewbyid(r.id.debj_total_interest);
}
private list<map<string,object>> cal_debx(){
/* <!--等额本息-->
每月还款总额=贷款本金×[月利率×(1+月利率)^还款月数]÷[(1+月利率)^还款月数-1]
每月应还本金=贷款本金×月利率×(1+月利率)^(还款月序号-1)÷〔(1+月利率)^还款月数-1〕
每月应还利息=贷款本金×月利率×〔(1+月利率)^还款月数-(1+月利率)^(还款月序号-1)〕÷〔(1+月利率)^还款月数-1〕
总利息=还款月数×每月还款总额-贷款本金
*/
string allloan=allloan.gettext().tostring().trim();//贷款多少
string yearinterestrate=yearinterestrate.gettext().tostring().trim();//年利率
string loanyear=loanyear.gettext().tostring().trim();//贷款年数
if (!allloan.equals("") && !yearinterestrate.equals("") && !loanyear.equals("")){
double allloan=double.parsedouble(allloan);//贷款多少
double yearinterestrate=double.parsedouble(yearinterestrate);//年利率
double monthinterestrate=yearinterestrate/12;//月利率
double loanyear=double.parsedouble(loanyear);//贷款年数
double loanmonth=loanyear*12;//还款月数
//......需要设置还款月序号
//......需要已归还本金累计额
//......需要剩余本金
list<map<string,object>> debx_list=new arraylist<>();
for (int i=1;i<=(int)loanmonth;i++){
map<string,object> map=new hashmap<>();
// <!--等额本息-->
//每月还款总额=贷款本金×[月利率×(1+月利率)^还款月数]÷[(1+月利率)^还款月数-1]
double debxmonthloan=new bigdecimal(allloan*monthinterestrate*math.pow((1+monthinterestrate),loanmonth)/(math.pow((1+monthinterestrate),loanmonth)-1)).setscale(2,bigdecimal.round_half_up).doublevalue();
//每月应还本金=贷款本金×月利率×(1+月利率)^(还款月序号-1)÷〔(1+月利率)^还款月数-1〕
double debxmonthprincipal=new bigdecimal(allloan*monthinterestrate*math.pow((1+monthinterestrate),(i-1))/(math.pow((1+monthinterestrate),loanmonth)-1)).setscale(2,bigdecimal.round_half_up).doublevalue();
//每月应还利息=贷款本金×月利率×〔(1+月利率)^还款月数-(1+月利率)^(还款月序号-1)〕÷〔(1+月利率)^还款月数-1〕
double debxmonthinterest=new bigdecimal(allloan*monthinterestrate*((math.pow((1+monthinterestrate),loanmonth))-math.pow((1+monthinterestrate),(i-1)))/(math.pow((1+monthinterestrate),loanmonth)-1)).setscale(2,bigdecimal.round_half_up).doublevalue();
map.put("debxmonth",string.valueof(i)+"月");
map.put("debxmonthloan",string.valueof(debxmonthloan));
map.put("debxmonthprincipal",string.valueof(debxmonthprincipal));
map.put("debxmonthinterest",string.valueof(debxmonthinterest));
debx_list.add(map);
}
//每月还款总额
double debxmonthloan=new bigdecimal(allloan*monthinterestrate*math.pow((1+monthinterestrate),loanmonth)/(math.pow((1+monthinterestrate),loanmonth)-1)).setscale(2,bigdecimal.round_half_up).doublevalue();
//总利息=还款月数×每月还款总额-贷款本金
double debxinterest=new bigdecimal(loanmonth*debxmonthloan-allloan).setscale(2,bigdecimal.round_half_up).doublevalue();
debxtotalinterest.settext(string.valueof(debxinterest));
return debx_list;
}else{
toast.maketext(this, "先输入与选择内容", toast.length_short).show();
}
return null;
}
private list<map<string,object>> cal_debj() {
/* <!--等额本金-->
每月还款总额=(贷款本金÷还款月数)+(贷款本金-已归还本金累计额)×月利率
每月应还本金=贷款本金÷还款月数
每月应还利息=剩余本金×月利率=(贷款本金-已归还本金累计额)×月利率。
每月月供递减额=每月应还本金×月利率=贷款本金÷还款月数×月利率
总利息=还款月数×(总贷款额×月利率-月利率×(总贷款额÷还款月数)*(还款月数-1)÷2+总贷款额÷还款月数)
*/
string allloan = allloan.gettext().tostring().trim();//贷款多少
string yearinterestrate = yearinterestrate.gettext().tostring().trim();//年利率
string loanyear = loanyear.gettext().tostring().trim();//贷款年数
if (!allloan.equals("") && !yearinterestrate.equals("") && !loanyear.equals("")) {
double allloan = double.parsedouble(allloan);//贷款多少
double yearinterestrate = double.parsedouble(yearinterestrate);//年利率
double monthinterestrate = yearinterestrate / 12;//月利率
double loanyear = double.parsedouble(loanyear);//贷款年数
double loanmonth = loanyear * 12;//还款月数
//......需要已归还本金累计额
//......需要剩余本金
list<map<string, object>> debj_list = new arraylist<>();
for (int i = 1; i <= (int) loanmonth; i++) {
map<string, object> map = new hashmap<>();
// <!--等额本金-->
//每月应还本金=贷款本金÷还款月数
double debjmonthprincipal = new bigdecimal(allloan / loanmonth).setscale(2, bigdecimal.round_half_up).doublevalue();
//每月还款总额=(贷款本金÷还款月数)+(贷款本金-累计已还款本金)×月利率
double debjmonthloan = new bigdecimal((allloan / loanmonth) + (allloan - debjmonthprincipal*i) * monthinterestrate).setscale(2, bigdecimal.round_half_up).doublevalue();
//每月应还利息=剩余本金×月利率=(贷款本金-累计已还款本金)×月利率。
double debjmonthinterest = new bigdecimal((allloan-debjmonthprincipal*i) * monthinterestrate).setscale(2, bigdecimal.round_half_up).doublevalue();
//每月月供递减额=每月应还本金×月利率=贷款本金÷还款月数×月利率
double debjmonthdecrease = new bigdecimal(debjmonthprincipal * monthinterestrate).setscale(2, bigdecimal.round_half_up).doublevalue();
map.put("debjmonth",string.valueof(i)+"月");
map.put("debjmonthloan",string.valueof(debjmonthloan));
map.put("debjmonthprincipal",string.valueof(debjmonthprincipal));
map.put("debjmonthinterest",string.valueof(debjmonthinterest));
map.put("debjmonthdecrease",string.valueof(debjmonthdecrease));
debj_list.add(map);
}
//总利息=还款月数×(总贷款额×月利率-月利率×(总贷款额÷还款月数)*(还款月数-1)÷2+总贷款额÷还款月数)
double debjinterest = new bigdecimal(((allloan/loanmonth+allloan*monthinterestrate)+allloan/loanmonth*(1+monthinterestrate))/2*loanmonth-allloan).setscale(2, bigdecimal.round_half_up).doublevalue();
debjtotalinterest.settext(string.valueof(debjinterest));
return debj_list;
} else {
toast.maketext(this, "先输入与选择内容", toast.length_short).show();
}
return null;
}
}xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_margin="15sp" android:layout_width="match_parent" android:layout_height="match_parent"> <linearlayout android:layout_marginbottom="15sp" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <textview android:text="贷款年数" android:textsize="14sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <edittext android:text="20" android:inputtype="number" android:layout_weight="1" android:id="@+id/loan_year" android:layout_width="match_parent" android:layout_height="wrap_content"/> <textview android:text="年利率" android:textsize="14sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <edittext android:text="0.0635" android:inputtype="number" android:layout_weight="1" android:id="@+id/year_interest_rate" android:layout_width="match_parent" android:layout_height="wrap_content"/> </linearlayout> <linearlayout android:gravity="center|left" android:layout_marginbottom="10sp" android:layout_width="match_parent" android:layout_height="wrap_content" tools:ignore="rtlhardcoded"> <textview android:text="贷款多少" android:textsize="14sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <edittext android:inputtype="number" android:layout_marginend="10sp" android:text="180000" android:id="@+id/all_loan" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content"/> <button android:background="@drawable/button_style" android:id="@+id/cal_loan" android:text="计算" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </linearlayout> <linearlayout android:layout_marginbottom="5sp" android:layout_width="match_parent" android:layout_height="wrap_content"> <textview android:layout_marginend="10sp" android:text="[等额本息]" android:textsize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <textview android:text="总利息: " android:layout_width="wrap_content" android:layout_height="wrap_content"/> <textview android:id="@+id/debx_total_interest" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </linearlayout> <linearlayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <textview android:layout_weight="1" android:text="每月总还款" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <textview android:layout_weight="1" android:text="每月还本金" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <textview android:layout_weight="1" android:text="每月还利息" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </linearlayout> <listview android:layout_weight="1" android:id="@+id/show_debx" android:layout_width="match_parent" android:layout_height="wrap_content"/> <linearlayout android:layout_margintop="15sp" android:layout_marginbottom="5sp" android:layout_width="match_parent" android:layout_height="wrap_content"> <textview android:layout_marginend="15sp" android:text="[等额本金]" android:textsize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <textview android:text="总利息:" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <textview android:id="@+id/debj_total_interest" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </linearlayout> <linearlayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <textview android:layout_weight="1" android:text="月总还款" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <textview android:layout_weight="1" android:text="月还本金" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <textview android:layout_weight="1" android:text="月还利息" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <textview android:layout_weight="1" android:text="月供递减" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </linearlayout> <listview android:layout_weight="1" android:id="@+id/show_debj" android:layout_width="match_parent" android:layout_height="wrap_content"/> </linearlayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。
看完文章,还可以扫描下面的二维码下载快手极速版领4元红包
除了扫码领红包之外,大家还可以在快手极速版做签到,看视频,做任务,参与抽奖,邀请好友赚钱)。
邀请两个好友奖最高196元,如下图所示:







