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運行效果如下:
如果有好的修改意見或建議,歡迎批評指正。