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

Android实现房贷计算器功能

34资源网2022-01-12474

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

分享给朋友:

相关文章

调整心态温暖哲理经典语录,看这些语录能够调整心态

调整心态温暖哲理经典语录,看这些语录能够调整心态

取得成功是他人不成功时仍在坚持不懈。若自身不作出一点外貌,别人想拉你一把都不知道你的手在哪儿。直至你不会再找我聊,直至你找不着我,直至最终,你一直在某一瞬间猛地想起我。但是,那个时候,被你弄丢的我就确实早已没有了,也再不想要你再找回家了。理…

逍遥手机模拟器怎么用(逍遥安卓模拟器详细使用教程)

逍遥手机模拟器怎么用(逍遥安卓模拟器详细使用教程)

真正的5V5公平竞技对战,传承端游纯正体验。人气英雄,经典还原;公平竞技,实力至上;峡谷传说,掌心再现。策略、战术、意识、配合,在移动端重现峡谷战场乐趣。 为了庆祝大家期待已久的中国区开服,官方也带来了五大福利活动,用户可免费参与,并获得十…

itunes备份路径在哪里(教你查看itunes备份路径)

itunes备份路径在哪里(教你查看itunes备份路径)

用itunes备份的都知道,备份到C盘的,日积月累,C盘越来越小了,上网教程参差不齐,本教程本人亲测,因为我看到我的64G SSD空间越来越小,伤不起!!!操作步骤1、下载Junction,将Junction.exe拷贝到C:Windows…

为了让米粉买到好看的手机壳,雷军投了一家“潮玩工厂”

为了让米粉买到好看的手机壳,雷军投了一家“潮玩工厂”

小米团队花了几个月时间,想要找到一个能DIY打印手机壳的设备却没有找到。直到2020年底,他们在西单大悦城看到玩壳工厂。 “用户需求一直存在,早前也有不少团队尝试开发这样的设备,但都失败了。”玩壳工厂创始人CEO韩冰告诉创业邦。 如今,玩壳…

带货主播成正式工种;AITO汽车来了;小米、英特尔入股张艺谋等创办的VR公司 ;特斯拉推出儿童电动车丨邦早报

带货主播成正式工种;AITO汽车来了;小米、英特尔入股张艺谋等创办的VR公司 ;特斯拉推出儿童电动车丨邦早报

【电子驾驶证12月10日起全国全面推行】据公安部消息,12月10日起,电子驾驶证在全国全面推行。此前,驾驶证电子化已覆盖北京、上海、广州、西安等200个城市,5000多万名驾驶人领取了电子驾驶证,实现手机在线“亮证”。电子驾驶证通过全国“交…