Android实现房贷计算器功能

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

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

(0)

相关推荐

  • Android开发实现的简单计算器功能【附完整demo源码下载】

    本文实例讲述了Android开发实现的简单计算器功能.分享给大家供大家参考,具体如下: 这个Android计算器虽然还有点小bug,不过简单的计算功能还是没问题的哦: 先上图看效果 比较简单,所以我就没怎么写注释,应该一看就能明白的 有不明白的可以发信问我 先贴MainActivity.java代码 package com.example.calculator; import android.app.Activity; import android.os.Bundle; import andro

  • Android Studio实现简易计算器

    如果是制作简易计算器的话是基本没有难点的,供大家参考,具体内容如下 步骤是先写好界面布局,将按钮的布局.字号颜色啥的做好,再就是设置监听器. 使用了NoTitleBar的主题 代码如下: activity_main里关于界面的代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/a

  • Android中使用GridLayout网格布局来制作简单的计算器App

    关于GridLayout 在android4.0版本之前,如果想要达到网格布局的效果,首先可以考虑使用最常见的LinearLayout布局,但是这样的排布会产生如下几点问题: 1.不能同时在X,Y轴方向上进行控件的对齐. 2.当多层布局嵌套时会有性能问题. 3.不能稳定地支持一些支持自由编辑布局的工具. 其次考虑使用表格布局TabelLayout,这种方式会把包含的元素以行和列的形式进行排列,每行为一个TableRow对象,也可以是一个View对象,而在TableRow中还可以继续添加其他的控件

  • Android studio设计简易计算器

    本文实例为大家分享了Android studio设计简易计算器的具体代码,供大家参考,具体内容如下 效果显示: 第一步,简单的界面布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.and

  • android计算器简单实现代码

    本文实例为大家分享了android计算器的具体实现代码,供大家参考,具体内容如下 java代码: package com.itheima74.simplecalculator4; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; imp

  • android studio实现计算器

    本文实例为大家分享了android studio实现计算器的具体代码,供大家参考,具体内容如下 效果图: 资源文件: color.xml <?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark&

  • 从零开始学android实现计算器功能示例分享(计算器源码)

    下面是效果展示: 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match

  • Android计算器编写代码

    其实这个安卓计算机,所有的后台思想与<C#计算器编写代码>是一模一样的.Win窗体程序移植到安卓,从C#到Java其实很简单的,因为两者的基本语法都很相像,唯一的难点是安卓的xml布局部分,不像C#窗体能够直接拖.  还是如下图一个能够完成基本四则运算的计算器: 先在res\values\strings.xml设置按钮相应的字体,以免布局文件警告满天飞: <?xml version="1.0" encoding="utf-8"?> <r

  • android计时器,时间计算器的实现方法

    需求:默认为"00:00:00",点击开始按钮时清零后开始计时,出现如10:28:34.点击停止的时候停止计时.问题:使用Calendar DateFormat的方法,不设置时区获取到的小时是本地时区的(东八区的就是8),设置成GMT标准时区获取到的时间是12小时(12:00:00),设置24小时制无效.在开始时间加减各种小时都无效,而且计时只能到12小时就自动跳上去了,始终无法出现默认状态00:00:00开始计时的效果.尝试各种时间设置方法无效后只能自己写一个根据秒数转换时间格式字符

  • Android实现简易计算器小程序

    本文实例为大家分享了Android实现简易计算器小程序的具体代码,供大家参考,具体内容如下 目标效果: 通过编写代码,可以实现整数和小数的加减乘除运算,以及删除和清空的功能. 1.页面中Button使用的是线性布局,最外边一个是父布局,第一行C,DEL,/,*为第一个子布局,第二行7,8,9,-为第二个子布局,第三行4,5,6,+为第三个子布局,第四五行为第四个子布局,第四个子布局中还有两个相当于是孙布局的级别,1,2,3为第一个孙布局,0和.为第二个孙布局,=在两个孙布局之外第四个子布局以内.

随机推荐