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

Android实现房贷计算器功能

34资源网2022-01-12559

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

分享给朋友:

相关文章

苹果X快充PD充电器推荐,现在下载还有优惠券可以领呢

苹果X快充PD充电器推荐,现在下载还有优惠券可以领呢

现在有很多人用的是苹果手机,因为平时有些人不注意使用方式或者使用充电器比较频繁导致损坏。大家都知道苹果手机和安卓手机的充电器是不一样的,不能用安卓手机的充电器充苹果手机。所以,大家如果苹果手机充电器损坏了,需要购买的话就要买个专门的苹果手机…

单身想找个女朋友,男的去哪里可以找个女朋友

单身想找个女朋友,男的去哪里可以找个女朋友

现在中国的男女比例失调,男的光棍要比女的多出3000w以上,这是个什么概念?代表着有3000w人是找不到对象的。所以很多单身男的就开始发愁了,单身想找个女朋友究竟到哪里找呢?说实话,小编也是一名单身汉,也正在找女朋友,虽然说,我没有找到女朋…

手机拍视频软件哪个好用(新手拍好人像摄影技巧)

手机拍视频软件哪个好用(新手拍好人像摄影技巧)

每一个热爱生活的人都关注了“手机摄影技巧”…

闪客快跑2背景音乐(闪客快打andylaw的微博)

闪客快跑2背景音乐(闪客快打andylaw的微博)

《疯狂跑酷》是一款LowPoly(低多边形)画风的跑酷游戏,一场突如其来的大水淹没了城市,而你在游戏中扮演一名刚下班的男子,需要从被水淹没的城市中逃出生天。…

联想乐pad平板电脑有哪些版本(平板电脑排行榜性价比)

联想乐pad平板电脑有哪些版本(平板电脑排行榜性价比)

对于安卓平板电脑行业而言,创新相对来说比较困难。一方面,安卓系统的功能早已经被各方挖掘殆尽;另一方面,从整个平板电脑行业大环境来看,iPad阵营坚固的护城河也影响着安卓阵营的创新欲望。再加上时下热衷于投身平板电脑行业的品牌本来就不像以往那么…

一周涨粉几百万,“张同学”凭什么火?

一周涨粉几百万,“张同学”凭什么火?

编者按:本文来自微信公众号时趣研究院(ID:SocialTouch2020),作者:时有趣,创业邦经授权转载 在最近的一段时间里,名叫“张同学”的博主刷屏了抖音,相关话题频频登上热榜,甚至还得到了人民网的点评。 张同学第一个视频的发布日期…