Android实战项目:房贷计算器

APP源码已上传到我的GitHub:https://github.com/zdm-code/Android-learning/tree/master/android_learning/mortgage

如今楼市可真是疯狂,房价蹭蹭的坐火箭飞涨,说到买房,自然少不了房贷,根据不同的贷款方式与还款方式,计算出来的月供数额各不相同,如果手机上有个房贷计算器,那可真是帮了不少人的忙。接下来就让我们瞅瞅这货好不好使

虽说Android进度并不算太多,但是根据迄今为止学到的开发知识,足够写个房贷计算器APP了,UI界面如下图所示:

用到的布局方式是相对布局,有什么控件这里不再多说,大家一目了然。

接下来说说控件的处理逻辑与房贷的计算逻辑

关于控件处理,两个按钮要触发不同的点击事件。单选按钮用来控制用户的还款方式,多选按钮用来控制贷款方式。不同的还款方式和贷款方式对应计算方法也是不同的,这些都要在计算按钮被按下时进行判断并写出各个分支情况。下面是贷款年限和基准利率的各种条件,复选框以dialog的形式展示,更能提高用户体验

   

复选框使用数组适配器ArrayAdapter的形式添加,String数组资源文件通过xml文件保存

 

通过读取item值然后从中截取字符串的方式获取利率和年份,以此来处理不同条件下的贷款计算方式。

对EditText控件的数据格式验证是必要的,如果用户输入的不是合法数字将导致APP崩溃。在这里我们使用输入时对非数字及小数点进行强制控制的方式,即当用户在软键盘上点击字母或非小数点的其他字符时,输入框内容不会发生任何变化。对多小数点和以0开头的数字的监控放到了按钮点击事件中,因为用户可能在输入过程中输入不合法的数,但是经过某种变换为合法,如果强制其不能输入,会导致糟糕的体验感(比如,现有数字123,当用户在最前端添加小数点或0(.123或0123),此时数字变为不合法,但是用户本意是要写成0.123,这个过程中就不能对这个输入做数字方面的合法校验)。除了对基本数据格式的验证外,如果用户贷款方式有两种,还需要对两种贷款额度的总和进行判断,如果总和不等于之前计算出的贷款总额,需要提示用户重新输入。

下面说一下贷款的计算方法

(内容来源于百度知道)

通过以上公式可以看到,所有我们控件中输入或计算得到的数值都可以直接拿来使用。

下面给出Spinner以数组适配器方式初始化的代码:

 1 private void initSpinner(){
 2         //初始化控件
 3         spinner1= (Spinner) findViewById(R.id.sp1);
 4         spinner2= (Spinner) findViewById(R.id.sp2);
 5         //建立数据源
 6         String[] years=getResources().getStringArray(R.array.years);
 7         String[] baserates=getResources().getStringArray(R.array.baserate);
 8         //声明一个下拉列表的数组适配器并绑定数据源
 9         ArrayAdapter<String> yearAdapter=new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,years);
10         ArrayAdapter<String> baserateAdapter=new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,baserates);
11         //绑定Adapter到控件
12         spinner1.setAdapter(yearAdapter);
13         spinner2.setAdapter(baserateAdapter);
14         //设置默认选择第一项
15         spinner1.setSelection(0);
16         spinner2.setSelection(0);
17         //设置标题
18         spinner1.setPrompt("请选择贷款年限");
19         spinner2.setPrompt("请选择基准利率");
20     }

以及控件处理数据逻辑代码:

  1 //给第一个计算按钮添加点击监听
  2         total.setOnClickListener(new View.OnClickListener() {
  3             @Override
  4             public void onClick(View view) {
  5                 buytotal=row1edit.getText().toString();
  6                 percent=row2edit.getText().toString();
  7                 if(TextUtils.isEmpty(buytotal)||TextUtils.isEmpty(percent))//判断前两个输入框是否非空
  8                 {
  9                     Toast.makeText(MainActivity.this,"购房总价和按揭部分信息填写完整",Toast.LENGTH_LONG).show();
 10                 }else if(TextUtil.isNum(buytotal)==false||TextUtil.isNum(percent)==false){//判断输入的是否是数字
 11                     Toast.makeText(MainActivity.this,"包含不合法的输入信息",Toast.LENGTH_LONG).show();
 12                 } else if(Double.parseDouble(percent)>100){//判断百分比部分输入是否大于100%
 13                     Toast.makeText(MainActivity.this,"按揭部分不能超过100%",Toast.LENGTH_LONG).show();
 14                 } else if(TextUtil.isNum(buytotal)&&TextUtil.isNum(percent))
 15                 {
 16                     intotal=(Double.parseDouble(buytotal)*Double.parseDouble(percent)*0.01);
 17                     totalcal.setText("您的贷款总额为"+String.format("%.2f",intotal)+"万元");
 18                 }
 19             }
 20         });
 21 
 22         detail.setOnClickListener(new View.OnClickListener() {
 23             @Override
 24             public void onClick(View view) {
 25                 //first,second为商贷和公积金贷所填数值
 26                 String first=row4edit.getText().toString();
 27                 String second=row5edit.getText().toString();
 28                 //firstrate和secondrate为商贷和公积金的年利率
 29                 double firstrate=Double.parseDouble(spinner2.getSelectedItem().toString().substring(20,24))*0.01;
 30                 double secondrate=Double.parseDouble(spinner2.getSelectedItem().toString().substring(31,35))*0.01;
 31                 //获取下拉列表的年份求得月份
 32                 String year=spinner1.getSelectedItem().toString();
 33                 month=Integer.parseInt(year.substring(0,year.length()-1))*12;
 34                 //两种贷款的月利率
 35                 double firstmonthrate=firstrate/12;
 36                 double secondmonthrate=secondrate/12;
 37                 if(totalcal.getText().toString().equals("其中贷款部分为:***万")){//判断是否计算过贷款总额
 38                     Toast.makeText(MainActivity.this,"请先计算贷款总额",Toast.LENGTH_LONG).show();
 39                 }else if(row1edit.getText().toString().equals(buytotal)==false||row2edit.getText().toString().equals(percent)==false){//监听贷款总额和按揭部分数值是否发生变化
 40                     Toast.makeText(MainActivity.this,"检查到您的购房总价或按揭部分数据更改,请重新计算贷款总额",Toast.LENGTH_LONG).show();
 41                 } else if(checkBox1.isChecked()==false&&checkBox2.isChecked()==false)//监听勾选的多选框
 42                 {
 43                     Toast.makeText(MainActivity.this,"请勾选贷款种类",Toast.LENGTH_LONG).show();
 44                 }else if(checkBox1.isChecked()&&checkBox2.isChecked()==false){
 45                     //等额本息贷款算法
 46                     if(radioGroup.getCheckedRadioButtonId()==R.id.btn1){
 47                         pertime=intotal*firstmonthrate*Math.pow((1+firstmonthrate),month)/(Math.pow(1+firstmonthrate,month)-1);
 48                         backtotal=pertime*month;
 49                         extra=backtotal-intotal;
 50                         alldetail.setText("您的贷款总额为"+String.format("%.2f",intotal)+"万元\n还款总额为"+String.format("%.2f",backtotal)+"万元\n其中利息总额为"+String.format("%.2f",extra)+"万元\n还款总时间为"+month+"月\n每月还款金额为"+String.format("%.2f",pertime*10000)+"元");
 51                     }else{//等额本金贷款算法
 52                         double[] array=new double[month];
 53                         double sum=0;
 54                         for(int i=0;i<month;i++)
 55                         {
 56                             array[i]=intotal/month+(intotal-sum)*firstmonthrate;
 57                             sum+=array[i];
 58                         }
 59                         String text="";
 60                         for(int i=0;i<month;i++){
 61                             text+="\n第"+(i+1)+"个月应还金额为:"+String.format("%.2f",array[i]*10000);
 62                         }
 63                         backtotal=sum;
 64                         extra=backtotal-intotal;
 65                         alldetail.setText("您的贷款总额为"+String.format("%.2f",intotal)+"万元\n还款总额为"+String.format("%.2f",backtotal)+"万元\n其中利息总额为"+String.format("%.2f",extra)+"万元\n还款总时间为"+month+"月\n每月还款金额如下:"+text);
 66                     }
 67 
 68                 }else if(checkBox1.isChecked()==false&&checkBox2.isChecked()){
 69                     if(radioGroup.getCheckedRadioButtonId()==R.id.btn1){
 70                         pertime=intotal*secondmonthrate*Math.pow((1+secondmonthrate),month)/(Math.pow(1+secondmonthrate,month)-1);
 71                         backtotal=pertime*month;
 72                         extra = backtotal - intotal;
 73                         alldetail.setText("您的贷款总额为" + String.format("%.2f",intotal) + "万元\n还款总额为"+String.format("%.2f",backtotal)+"万元\n其中利息总额为"+String.format("%.2f",extra)+"万元\n还款总时间为"+month+"月\n每月还款金额为"+String.format("%.2f",pertime*10000)+"元");
 74                     }else{
 75                         double[] array=new double[month];
 76                         double sum=0;
 77                         for(int i=0;i<month;i++)
 78                         {
 79                             array[i]=intotal/month+(intotal-sum)*secondmonthrate;
 80                             sum+=array[i];
 81                         }
 82                         String text="";
 83                         for(int i=0;i<month;i++){
 84                             text+="\n第"+(i+1)+"个月应还金额为:"+String.format("%.2f",array[i]*10000)+"元";
 85                         }
 86                         backtotal=sum;
 87                         extra=backtotal-intotal;
 88                         alldetail.setText("您的贷款总额为"+String.format("%.2f",intotal)+"万元\n还款总额为"+String.format("%.2f",backtotal)+"万元\n其中利息总额为"+String.format("%.2f",extra)+"万元\n还款总时间为"+month+"月\n每月还款金额如下:"+text);
 89                     }
 90                 }else if(checkBox1.isChecked()&&checkBox2.isChecked()){
 91                     if(radioGroup.getCheckedRadioButtonId()==R.id.btn1){
 92                         if(TextUtils.isEmpty(first)||TextUtils.isEmpty(second)){
 93                             Toast.makeText(MainActivity.this,"请将空信息填写完整",Toast.LENGTH_LONG).show();
 94                         }else if(TextUtil.isNum(first)==false||TextUtil.isNum(second)==false){
 95                             Toast.makeText(MainActivity.this,"包含不合法的输入信息",Toast.LENGTH_LONG).show();
 96                         }else if(Double.parseDouble(first)+Double.parseDouble(second)!=Double.parseDouble(String.format("%.2f",intotal))){
 97                             Toast.makeText(MainActivity.this,"填写的两项贷款总额不等于初始贷款额度,请重新填写",Toast.LENGTH_LONG).show();
 98                         }else{
 99                             double p1=Double.parseDouble(first);
100                             double p2=Double.parseDouble(second);
101                             double pertime1=p1*firstmonthrate*Math.pow((1+firstmonthrate),month)/(Math.pow(1+firstmonthrate,month)-1);
102                             double pertime2=p2*secondmonthrate*Math.pow((1+secondmonthrate),month)/(Math.pow(1+secondmonthrate,month)-1);
103                             pertime=pertime1+pertime2;
104                             backtotal=pertime*month;
105                             extra=backtotal-intotal;
106                             alldetail.setText("您的贷款总额为" + String.format("%.2f",intotal) + "万元\n还款总额为"+String.format("%.2f",backtotal)+"万元\n其中利息总额为"+String.format("%.2f",extra)+"万元\n还款总时间为"+month+"月\n每月还款金额为"+String.format("%.2f",pertime*10000)+"元");
107                         }
108                     }else{
109                         if(first.equals("请输入商业贷款总额(单位万)")||second.equals("请输入公积金贷款总额(单位万)")){
110                             Toast.makeText(MainActivity.this,"请将空信息填写完整",Toast.LENGTH_LONG).show();
111                         }else if(TextUtil.isNum(first)==false||TextUtil.isNum(second)==false){
112                             Toast.makeText(MainActivity.this,"包含不合法的输入信息",Toast.LENGTH_LONG).show();
113                         }else if(Double.parseDouble(first)+Double.parseDouble(second)!=Double.parseDouble(String.format("%.2f",intotal))){
114                             Toast.makeText(MainActivity.this,"填写的两项贷款总额不等于初始贷款额度,请重新填写",Toast.LENGTH_LONG).show();
115                         }else{
116                             double p1=Double.parseDouble(first);
117                             double p2=Double.parseDouble(second);
118                             double[] array1=new double[month];
119                             double[] array2=new double[month];
120                             double sum1=0,sum2=0;
121                             for(int i=0;i<month;i++)
122                             {
123                                 array1[i]=p1/month+(p1-sum1)*firstmonthrate;
124                                 array2[i]=p2/month+(p2-sum2)*secondmonthrate;
125                                 Toast.makeText(MainActivity.this,array1[i]+" "+array2[i],Toast.LENGTH_LONG);
126                                 sum1+=array1[i];
127                                 sum2+=array2[i];
128                             }
129                             String text="";
130                             for(int i=0;i<month;i++){
131                                 text+="\n第"+(i+1)+"个月应还金额为:"+String.format("%.2f",(array1[i]+array2[i])*10000)+"元";
132                             }
133                             backtotal=sum1+sum2;
134                             extra=backtotal-intotal;
135                             alldetail.setText("您的贷款总额为"+String.format("%.2f",intotal)+"万元\n还款总额为"+String.format("%.2f",backtotal)+"万元\n其中利息总额为"+String.format("%.2f",extra)+"万元\n还款总时间为"+month+"月\n每月还款金额如下:"+text);
136                         }
137                     }
138                 }
139             }
140         });
141 
142         row1edit.addTextChangedListener(new TextWatcher() {
143             int oldlength=0;
144             @Override
145             public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
146 
147             }
148 
149             @Override
150             public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {//强制用户不能输入非数字和小数点之外的字符
151                 int length = charSequence.length();
152                 if (length > oldlength) {
153                     char newchar = charSequence.charAt(i);
154                     if ((newchar < '0' && newchar > '9') && newchar != '.') {
155                         if (i != length - 1)
156                             row1edit.setText(charSequence.subSequence(0, i).toString() + charSequence.subSequence(i + 1, length).toString());
157                         else
158                             row1edit.setText(charSequence.subSequence(0, length - 1));
159                     }
160                 }
161                 oldlength=length;
162             }
163 
164             @Override
165             public void afterTextChanged(Editable editable) {
166 
167             }
168         });

这里只给出一个EditText的输入时监听方法,其他输入框相同。APP运行效果如下:

       

 如果有好的修改意见或建议,欢迎批评指正。

猜你喜欢

转载自www.cnblogs.com/zdm-code/p/12206873.html