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

Android实现房贷计算器功能

34资源网2022-01-12456

本文实例为大家分享了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元,如下图所示:

快手极速版邀请好友奖励

扫描二维码推送至手机访问。

版权声明:本文由34楼发布,如需转载请注明出处。

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

分享给朋友:

相关文章

联合国秘书长:新冠疫苗必须要成为全球公共产品

联合国秘书长:新冠疫苗必须要成为全球公共产品

当地时间3月11日,联合国启动“必须团结一致”(Only Together)公共宣传活动,呼吁新冠疫苗能够在联合国疫苗计划主导下,成为全球公共产品,向全球各地有需要的人提供。…

令人受益的三种说话方式了解一下

令人受益的三种说话方式了解一下

有人说:人与人之间最累的关系就是感觉话不投机,说得越多越费劲,最后只能无话可说其实,想让别人喜欢听你说话,要先掌握正确的表达方式。你对别人说话的样子,决定了你在别人眼中的样子。…

天上的孩子电影好看吗?看豆瓣网友如何评价的吧

天上的孩子电影好看吗?看豆瓣网友如何评价的吧

由胡玫监制许磊导演编剧的电影《天上的孩子》。电影主要讲述了来自贵州的夫妻老何、玲霞5岁的独子查出绝症,不久于人世。为了让儿子的名字刻在纪念碑上,夫妻俩决定捐献儿子的器官却困难重重。…

谈2022年平衡发展策略,我们要平衡的发展

谈2022年平衡发展策略,我们要平衡的发展

2022年解决方案的第二个关键词叫平衡。我们要平衡的发展,长期的发展。你的个人生活,你的公司事业,还有国家的形势,我觉得一定要平衡的去发展,不能说我只赚钱,身体就搞垮,或者说我只赚快钱,但是不关心国家的形势。你看,密室逃脱这个行业,上周国家…

ic卡与id卡区别(卡片类型和原理的区别)

ic卡与id卡区别(卡片类型和原理的区别)

IC卡全称为集成电路卡,也称智能卡,特点是可读写,可加密,安全性高、容量大,可存储数据,常用于一卡通系统;ID卡全称为身份识别卡,这只是一种感应卡,这种卡只可读不可写,有固定的编号,常用于门禁和考勤系统。 IC卡可以说是ID卡的一种升级形…

ppp文件类型(ppp项目通俗解释)

ppp文件类型(ppp项目通俗解释)

在化解地方债务、融资渠道转型和各部委推动等多重因素驱动下,从2014下半年起国内各地兴起PPP热,工作开展较好的省份已有PPP项目开花结果,这让有意参与PPP运作的各市场主体眼前一亮,激发了更大兴趣。 比如金融机构,任何PPP项目的成功实施…