当前位置:首页 > 谈天说地

Android实现房贷计算器功能

34资源网2022-01-12590

本文实例为大家分享了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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。

版权声明:34楼发布

本文链接:https://www.34l.com/post/5247.html

分享给朋友:

相关文章

零成本创业项目,2020年就做这个最赚钱

零成本创业项目,2020年就做这个最赚钱

一场肺炎冠状病毒,让很多人没有了欲望,除了能活下去。曾经繁华的商业街,现在空无一人, 曾经热闹的小吃店,现在大门紧锁。那么,在这个时候还可以创业吗?当然是可以的。今天小编就给大家推荐一个适合零成本创业的项目,这个项目是玩手游赚钱。从现在开…

华梅的经典语录

华梅的经典语录

华梅,女,1951年生于天津市,祖籍无锡。现为天津师范大学美术与设计学院院长、教授、师大华梅服饰文化学研究所所长。国家人事部授衔“有突出贡献中青年专家”,享受国务院政府津贴。1997年天津市劳动模范,1998年全国教育系统巾帼建功标兵、全国…

g系列cpu性能排行(英特尔u系列和g系列)

g系列cpu性能排行(英特尔u系列和g系列)

去年10月,AMD正式发布Zen 3架构锐龙5000系列处理器,单线程和多线程性能实现“质”的飞跃,反超当时的10代酷睿处理器,与后来发布的11代酷睿处理器相比,也丝毫不落于下风。 在这样的大前提下,AMD于4月发布了Zen 3架构锐龙5…

个人微信号怎么更改(设置里的微信密码)

个人微信号怎么更改(设置里的微信密码)

有多少人和我一样,起了一个非常非常中二的名字。当初的年少无知终于可以后悔了。微信号终于!终于!终于可以修改微信号了。(IOS用户请先等等,现在仅支持安卓用户哦) 微信最近推出新功能,允许用户每年修改一次微信号。 TOP1:首先把微信更新到…

瑞幸喘口气,对手已林立

瑞幸喘口气,对手已林立

图源:摄图网 编者按:本文来自微信公众号螳螂观察(ID:TanglangFin),作者:易不二,创业邦经授权转载 年轻人从未停止消费小蓝杯。 今年夏天生椰拿铁被全网催货的盛景,与9天就突破270万杯销量的丝绒拿铁,都证明了瑞幸的生命力。 从…

联想之鉴,大厂“芯”领神会

联想之鉴,大厂“芯”领神会

编者按:本文转自光子星球,作者文烨豪,编辑吴先之,创业邦经授权转载。 近日,阿里云宣布,达摩院成功研发出存算一体芯片。腾讯造芯的波澜还未平复,阿里便再次出招,造芯似乎成为了互联网巨头们的新常态。 就拿2021年来说,百度宣布昆仑2芯片量产,…