本文实例为大家分享了android实现房贷计算器的具体代码,供大家参考,具体内容如下
fangdai(activity)
package com.example.myapplication_one; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.text.editable; import android.text.textutils; import android.text.textwatcher; import android.view.view; import android.widget.arrayadapter; import android.widget.button; import android.widget.checkbox; import android.widget.edittext; import android.widget.radiogroup; import android.widget.spinner; import android.widget.textview; import android.widget.toast; public class fangdai extends appcompatactivity { //声明用到的所有控件 spinner spinner1; spinner spinner2; edittext row1edit; edittext row2edit; button total; radiogroup radiogroup; checkbox checkbox1; checkbox checkbox2; edittext row4edit; edittext row5edit; button detail; textview totalcal; textview alldetail; private void initspinner(){ //初始化控件 spinner1= (spinner) findviewbyid(r.id.sp1); spinner2= (spinner) findviewbyid(r.id.sp2); //建立数据源 string[] years=getresources().getstringarray(r.array.years); string[] baserates=getresources().getstringarray(r.array.baserate); //声明一个下拉列表的数组适配器并绑定数据源 arrayadapter<string> yearadapter=new arrayadapter<string>(this,r.layout.support_simple_spinner_dropdown_item,years); arrayadapter<string> baserateadapter=new arrayadapter<string>(this,r.layout.support_simple_spinner_dropdown_item,baserates); //绑定adapter到控件 spinner1.setadapter(yearadapter); spinner2.setadapter(baserateadapter); //设置默认选择第一项 spinner1.setselection(0); spinner2.setselection(0); //设置标题 spinner1.setprompt("请选择贷款年限"); spinner2.setprompt("请选择基准利率"); } //声明下列函数中要用到的变量 double intotal,backtotal,extra,pertime;//贷款总额,还款总额,利息,每月还款总额 int month;//月份 string buytotal;//购房总额 string percent;//贷款百分比 @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.fangdai); //初始化控件 initspinner(); row1edit= (edittext) findviewbyid(r.id.row1edit); row2edit= (edittext) findviewbyid(r.id.row2edit); total= (button) findviewbyid(r.id.totalcal); radiogroup= (radiogroup) findviewbyid(r.id.radiogroup); checkbox1= (checkbox) findviewbyid(r.id.check1); checkbox2= (checkbox) findviewbyid(r.id.check2); totalcal= (textview) findviewbyid(r.id.showtotal); detail= (button) findviewbyid(r.id.detail); alldetail= (textview) findviewbyid(r.id.alldetail); row4edit= (edittext) findviewbyid(r.id.row4label); row5edit= (edittext) findviewbyid(r.id.row5label); //给第一个计算按钮添加点击监听 total.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { buytotal=row1edit.gettext().tostring(); percent=row2edit.gettext().tostring(); if(textutils.isempty(buytotal)||textutils.isempty(percent))//判断前两个输入框是否非空 { toast.maketext(fangdai.this,"购房总价和按揭部分信息填写完整",toast.length_long).show(); }else if(fangdaitext.isnum(buytotal)==false||fangdaitext.isnum(percent)==false){//判断输入的是否是数字 toast.maketext(fangdai.this,"包含不合法的输入信息",toast.length_long).show(); } else if(double.parsedouble(percent)>100){//判断百分比部分输入是否大于100% toast.maketext(fangdai.this,"按揭部分不能超过100%",toast.length_long).show(); } else if(fangdaitext.isnum(buytotal)&&fangdaitext.isnum(percent)) { intotal=(double.parsedouble(buytotal)*double.parsedouble(percent)*0.01); totalcal.settext("您的贷款总额为"+string.format("%.2f",intotal)+"万元"); } } }); detail.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { //first,second为商贷和公积金贷所填数值 string first=row4edit.gettext().tostring(); string second=row5edit.gettext().tostring(); //firstrate和secondrate为商贷和公积金的年利率 double firstrate=double.parsedouble(spinner2.getselecteditem().tostring().substring(20,24))*0.01; double secondrate=double.parsedouble(spinner2.getselecteditem().tostring().substring(31,35))*0.01; //获取下拉列表的年份求得月份 string year=spinner1.getselecteditem().tostring(); month=integer.parseint(year.substring(0,year.length()-1))*12; //两种贷款的月利率 double firstmonthrate=firstrate/12; double secondmonthrate=secondrate/12; if(totalcal.gettext().tostring().equals("其中贷款部分为:***万")){//判断是否计算过贷款总额 toast.maketext(fangdai.this,"请先计算贷款总额",toast.length_long).show(); }else if(row1edit.gettext().tostring().equals(buytotal)==false||row2edit.gettext().tostring().equals(percent)==false){//监听贷款总额和按揭部分数值是否发生变化 toast.maketext(fangdai.this,"检查到您的购房总价或按揭部分数据更改,请重新计算贷款总额",toast.length_long).show(); } else if(checkbox1.ischecked()==false&&checkbox2.ischecked()==false)//监听勾选的多选框 { toast.maketext(fangdai.this,"请勾选贷款种类",toast.length_long).show(); }else if(checkbox1.ischecked()&&checkbox2.ischecked()==false){ //等额本息贷款算法 if(radiogroup.getcheckedradiobuttonid()==r.id.btn1){ pertime=intotal*firstmonthrate*math.pow((1+firstmonthrate),month)/(math.pow(1+firstmonthrate,month)-1); backtotal=pertime*month; extra=backtotal-intotal; alldetail.settext("您的贷款总额为"+string.format("%.2f",intotal)+"万元\n还款总额为"+string.format("%.2f",backtotal)+"万元\n其中利息总额为"+string.format("%.2f",extra)+"万元\n还款总时间为"+month+"月\n每月还款金额为"+string.format("%.2f",pertime*10000)+"元"); }else{//等额本金贷款算法 double[] array=new double[month]; double sum=0; for(int i=0;i<month;i++) { array[i]=intotal/month+(intotal-sum)*firstmonthrate; sum+=array[i]; } string text=""; for(int i=0;i<month;i++){ text+="\n第"+(i+1)+"个月应还金额为:"+string.format("%.2f",array[i]*10000); } backtotal=sum; extra=backtotal-intotal; alldetail.settext("您的贷款总额为"+string.format("%.2f",intotal)+"万元\n还款总额为"+string.format("%.2f",backtotal)+"万元\n其中利息总额为"+string.format("%.2f",extra)+"万元\n还款总时间为"+month+"月\n每月还款金额如下:"+text); } }else if(checkbox1.ischecked()==false&&checkbox2.ischecked()){ if(radiogroup.getcheckedradiobuttonid()==r.id.btn1){ pertime=intotal*secondmonthrate*math.pow((1+secondmonthrate),month)/(math.pow(1+secondmonthrate,month)-1); backtotal=pertime*month; extra = backtotal - intotal; alldetail.settext("您的贷款总额为" + string.format("%.2f",intotal) + "万元\n还款总额为"+string.format("%.2f",backtotal)+"万元\n其中利息总额为"+string.format("%.2f",extra)+"万元\n还款总时间为"+month+"月\n每月还款金额为"+string.format("%.2f",pertime*10000)+"元"); }else{ double[] array=new double[month]; double sum=0; for(int i=0;i<month;i++) { array[i]=intotal/month+(intotal-sum)*secondmonthrate; sum+=array[i]; } string text=""; for(int i=0;i<month;i++){ text+="\n第"+(i+1)+"个月应还金额为:"+string.format("%.2f",array[i]*10000)+"元"; } backtotal=sum; extra=backtotal-intotal; alldetail.settext("您的贷款总额为"+string.format("%.2f",intotal)+"万元\n还款总额为"+string.format("%.2f",backtotal)+"万元\n其中利息总额为"+string.format("%.2f",extra)+"万元\n还款总时间为"+month+"月\n每月还款金额如下:"+text); } }else if(checkbox1.ischecked()&&checkbox2.ischecked()){ if(radiogroup.getcheckedradiobuttonid()==r.id.btn1){ if(textutils.isempty(first)||textutils.isempty(second)){ toast.maketext(fangdai.this,"请将空信息填写完整",toast.length_long).show(); }else if(fangdaitext.isnum(first)==false||fangdaitext.isnum(second)==false){ toast.maketext(fangdai.this,"包含不合法的输入信息",toast.length_long).show(); }else if(double.parsedouble(first)+double.parsedouble(second)!=double.parsedouble(string.format("%.2f",intotal))){ toast.maketext(fangdai.this,"填写的两项贷款总额不等于初始贷款额度,请重新填写",toast.length_long).show(); }else{ double p1=double.parsedouble(first); double p2=double.parsedouble(second); double pertime1=p1*firstmonthrate*math.pow((1+firstmonthrate),month)/(math.pow(1+firstmonthrate,month)-1); double pertime2=p2*secondmonthrate*math.pow((1+secondmonthrate),month)/(math.pow(1+secondmonthrate,month)-1); pertime=pertime1+pertime2; backtotal=pertime*month; extra=backtotal-intotal; alldetail.settext("您的贷款总额为" + string.format("%.2f",intotal) + "万元\n还款总额为"+string.format("%.2f",backtotal)+"万元\n其中利息总额为"+string.format("%.2f",extra)+"万元\n还款总时间为"+month+"月\n每月还款金额为"+string.format("%.2f",pertime*10000)+"元"); } }else{ if(first.equals("请输入商业贷款总额(单位万)")||second.equals("请输入公积金贷款总额(单位万)")){ toast.maketext(fangdai.this,"请将空信息填写完整",toast.length_long).show(); }else if(fangdaitext.isnum(first)==false||fangdaitext.isnum(second)==false){ toast.maketext(fangdai.this,"包含不合法的输入信息",toast.length_long).show(); }else if(double.parsedouble(first)+double.parsedouble(second)!=double.parsedouble(string.format("%.2f",intotal))){ toast.maketext(fangdai.this,"填写的两项贷款总额不等于初始贷款额度,请重新填写",toast.length_long).show(); }else{ double p1=double.parsedouble(first); double p2=double.parsedouble(second); double[] array1=new double[month]; double[] array2=new double[month]; double sum1=0,sum2=0; for(int i=0;i<month;i++) { array1[i]=p1/month+(p1-sum1)*firstmonthrate; array2[i]=p2/month+(p2-sum2)*secondmonthrate; toast.maketext(fangdai.this,array1[i]+" "+array2[i],toast.length_long); sum1+=array1[i]; sum2+=array2[i]; } string text=""; for(int i=0;i<month;i++){ text+="\n第"+(i+1)+"个月应还金额为:"+string.format("%.2f",(array1[i]+array2[i])*10000)+"元"; } backtotal=sum1+sum2; extra=backtotal-intotal; alldetail.settext("您的贷款总额为"+string.format("%.2f",intotal)+"万元\n还款总额为"+string.format("%.2f",backtotal)+"万元\n其中利息总额为"+string.format("%.2f",extra)+"万元\n还款总时间为"+month+"月\n每月还款金额如下:"+text); } } } } }); row1edit.addtextchangedlistener(new textwatcher() { int oldlength=0; @override public void beforetextchanged(charsequence charsequence, int i, int i1, int i2) { } @override public void ontextchanged(charsequence charsequence, int i, int i1, int i2) {//强制用户不能输入非数字和小数点之外的字符 int length = charsequence.length(); if (length > oldlength) { char newchar = charsequence.charat(i); if ((newchar < '0' && newchar > '9') && newchar != '.') { if (i != length - 1) row1edit.settext(charsequence.subsequence(0, i).tostring() + charsequence.subsequence(i + 1, length).tostring()); else row1edit.settext(charsequence.subsequence(0, length - 1)); } } oldlength=length; } @override public void aftertextchanged(editable editable) { } }); row2edit.addtextchangedlistener(new textwatcher() { int oldlength=0; @override public void beforetextchanged(charsequence charsequence, int i, int i1, int i2) { } @override public void ontextchanged(charsequence charsequence, int i, int i1, int i2) { int length=charsequence.length(); if(length>oldlength) { char newchar = charsequence.charat(i); if ((newchar < '0' && newchar > '9') && newchar != '.') { if (i != length - 1) row2edit.settext(charsequence.subsequence(0, i).tostring() + charsequence.subsequence(i + 1, length).tostring()); else row2edit.settext(charsequence.subsequence(0, length - 1)); } } oldlength=length; } @override public void aftertextchanged(editable editable) { } }); row4edit.addtextchangedlistener(new textwatcher() { int oldlength=0; @override public void beforetextchanged(charsequence charsequence, int i, int i1, int i2) { } @override public void ontextchanged(charsequence charsequence, int i, int i1, int i2) { int length=charsequence.length(); if(length>oldlength) { char newchar = charsequence.charat(i); if ((newchar < '0' && newchar > '9') && newchar != '.') { if (i != length - 1) row4edit.settext(charsequence.subsequence(0, i).tostring() + charsequence.subsequence(i + 1, length).tostring()); else row4edit.settext(charsequence.subsequence(0, length - 1)); } } oldlength=length; } @override public void aftertextchanged(editable editable) { } }); row5edit.addtextchangedlistener(new textwatcher() { int oldlength=0; @override public void beforetextchanged(charsequence charsequence, int i, int i1, int i2) { } @override public void ontextchanged(charsequence charsequence, int i, int i1, int i2) { int length=charsequence.length(); if(length>oldlength) { char newchar = charsequence.charat(i); if ((newchar < '0' && newchar > '9') && newchar != '.') { if (i != length - 1) row5edit.settext(charsequence.subsequence(0, i).tostring() + charsequence.subsequence(i + 1, length).tostring()); else row5edit.settext(charsequence.subsequence(0, length - 1)); } } oldlength=length; } @override public void aftertextchanged(editable editable) { } }); } }
fangdaitext(activity)
package com.example.myapplication_one; public class fangdaitext { public static boolean isnum(string string){ int flag=0; if(string.charat(0)=='0'&&string.charat(1)!='.') return false; if(string.charat(0)=='.') return false; for(int i=0;i<string.length();i++) { if((string.charat(i)<'0'||string.charat(i)>'9')&&string.charat(i)!='.') return false; else if(string.charat(i)=='.') { flag++; if(flag>1) return false; } } return true; } }
fangdai.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="16dp" android:focusableintouchmode="true" android:focusable="true" tools:context="com.example.myapplication_one.fangdai"> <scrollview android:layout_width="match_parent" android:layout_height="wrap_content"> <relativelayout android:layout_width="match_parent" android:layout_height="wrap_content"> <relativelayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/relativelayout1"> <textview android:id="@+id/row1label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="购房总价:" android:layout_centervertical="true" android:textsize="18sp" /> <edittext android:id="@+id/row1edit" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centervertical="true" android:hint="请输入购房总价(单位万)" android:singleline="true" android:textsize="16dp" android:background="@drawable/edittext_style" android:padding="5dp" android:gravity="right" android:layout_torightof="@+id/row1label" android:layout_toleftof="@+id/row1endlabel" android:inputtype="numberdecimal"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="万" android:textsize="18sp" android:layout_marginleft="10dp" android:layout_centervertical="true" android:layout_alignparentend="true" android:id="@+id/row1endlabel" android:layout_alignparentright="true" /> </relativelayout> <relativelayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/relativelayout2" android:layout_below="@+id/relativelayout1" android:layout_margintop="20dp"> <textview android:id="@+id/row2label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按揭部分:" android:layout_centervertical="true" android:textsize="18sp" /> <edittext android:id="@+id/row2edit" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centervertical="true" android:hint="请输入按揭百分比" android:singleline="true" android:textsize="16dp" android:background="@drawable/edittext_style" android:padding="5dp" android:gravity="right" android:layout_torightof="@+id/row2label" android:layout_toleftof="@+id/row2endlabel" android:inputtype="numberdecimal"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" %" android:textsize="18sp" android:layout_marginleft="10dp" android:layout_centervertical="true" android:layout_alignparentend="true" android:id="@+id/row2endlabel" android:layout_alignparentright="true" /> </relativelayout> <button android:id="@+id/totalcal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/relativelayout2" android:layout_margintop="15dp" android:background="@drawable/btn_style" android:text="计算贷款总额" /> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:text="其中贷款部分为:***万" android:layout_below="@+id/totalcal" android:layout_margintop="10dp" android:textsize="16sp" android:id="@+id/showtotal"/> <relativelayout android:id="@+id/relativelayout3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/showtotal" android:layout_margintop="10dp"> <textview android:id="@+id/row3label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="还款方式:" android:textsize="16sp" android:layout_centervertical="true"/> <radiogroup android:id="@+id/radiogroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_torightof="@+id/row3label" android:orientation="horizontal"> <radiobutton android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="等额本息" android:checked="true"/> <radiobutton android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="等额本金" android:layout_marginleft="10dp"/> </radiogroup> </relativelayout> <relativelayout android:id="@+id/relativelayout4" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/relativelayout3"> <checkbox android:id="@+id/check1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="商贷: "/> <edittext android:id="@+id/row4label" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centervertical="true" android:hint="请输入商业贷款总额(单位万)" android:singleline="true" android:textsize="16dp" android:background="@drawable/edittext_style" android:padding="5dp" android:gravity="right" android:layout_torightof="@+id/check1" android:layout_toleftof="@+id/row4endlabel" android:inputtype="numberdecimal"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="万" android:textsize="18sp" android:layout_marginleft="10dp" android:layout_centervertical="true" android:layout_alignparentend="true" android:id="@+id/row4endlabel"/> </relativelayout> <relativelayout android:id="@+id/relativelayout5" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/relativelayout4" android:layout_margintop="5dp"> <checkbox android:id="@+id/check2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="公积金:"/> <edittext android:id="@+id/row5label" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centervertical="true" android:hint="请输入公积金贷款总额(单位万)" android:singleline="true" android:textsize="16dp" android:background="@drawable/edittext_style" android:padding="5dp" android:gravity="right" android:layout_torightof="@+id/check2" android:layout_toleftof="@+id/row5endlabel" android:inputtype="numberdecimal"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="万" android:textsize="18sp" android:layout_marginleft="10dp" android:layout_centervertical="true" android:layout_alignparentend="true" android:id="@+id/row5endlabel" /> </relativelayout> <relativelayout android:id="@+id/relativelayout6" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/relativelayout5" android:layout_margintop="10dp"> <textview android:id="@+id/row6label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="贷款年限:" android:textsize="16sp" android:layout_centervertical="true"/> <spinner android:id="@+id/sp1" android:layout_centervertical="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:spinnermode="dialog" android:layout_torightof="@+id/row6label"> </spinner> </relativelayout> <relativelayout android:id="@+id/relativelayout7" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/relativelayout6" android:layout_margintop="10dp"> <textview android:id="@+id/row7label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="基准利率:" android:layout_centervertical="true" android:textsize="16sp"/> <spinner android:id="@+id/sp2" android:layout_width="match_parent" android:layout_height="wrap_content" android:spinnermode="dialog" android:layout_centervertical="true" android:layout_torightof="@+id/row7label"> </spinner> </relativelayout> <button android:id="@+id/detail" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/relativelayout7" android:layout_margintop="15dp" android:background="@drawable/btn_style" android:text="计算还款明细"/> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:text="还款总额为:***万\n其中利息总额为:***万\n月供(每月还款额)为:***" android:layout_below="@+id/detail" android:layout_margintop="10dp" android:textsize="16sp" android:id="@+id/alldetail"/> </relativelayout> </scrollview> </relativelayout>
edittext_style.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true"> <shape> <corners android:radius="5dp"/> <stroke android:width="1dp" android:color="#00ddff"/> </shape> </item> <item android:state_focused="false"> <shape> <corners android:radius="5dp"/> <stroke android:width="1dp" android:color="#000000"/> </shape> </item> </selector>
btn_style.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <shape> <solid android:color="#808080"/> <corners android:radius="2dp"/> <stroke android:width="1dp" android:color="@color/teal_200"/> </shape> </item> <item android:state_pressed="false"> <shape> <solid android:color="#33e3f3"/> <corners android:radius="2dp"/> <stroke android:width="1dp" android:color="#07ac78"/> </shape> </item> </selector>
dimens.xml
<resources> <!-- default screen margins, per the android design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> </resources>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。