Android 汇率换算对比小工具

本文介绍了如何开发一个Android小工具,用于进行货币汇率换算。涵盖了应用的主要功能实现和关键代码片段。

效果
在这里插入图片描述

配置

apply plugin: 'kotlin-android-extensions'

代码

package com.kok.formoney

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.appcompat.widget.AppCompatButton
import kotlinx.android.synthetic.main.activity_main.*
import java.lang.Exception
import java.text.DecimalFormat

class MainActivity : AppCompatActivity() {

    var handleType = false; // true 表示高价值币种

    var type01 = 0;
    var type02 = 1;

    override fun onCreate(savedInstanceState: Bundle?) {
        window.statusBarColor = Color.parseColor("#528A63")
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
    }

    fun initView() {
        addItem()
        radioGroup_type.setOnCheckedChangeListener(checkChangeListener)
        radioGroup_type02.setOnCheckedChangeListener(checkChangeListener)
        button_add.setOnClickListener(onClickListener)
        button_count.setOnClickListener(onClickListener)
    }

    fun addItem() {
        var itenLayout = LinearLayout(this)
        var params = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        itenLayout.layoutParams = params
        itenLayout.orientation = LinearLayout.HORIZONTAL

        var hite = TextView(this)
        params = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        hite.layoutParams = params
        hite.text = "交换汇率:"
        itenLayout.addView(hite)

        var inputView = EditText(this)
        params = ViewGroup.LayoutParams(
            200,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        inputView.layoutParams = params
        itenLayout.addView(inputView)
        var resultView = TextView(this)
        var linearLayoutParams = LinearLayout.LayoutParams(
            0,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        linearLayoutParams.weight = 1f
        resultView.layoutParams = linearLayoutParams
        itenLayout.addView(resultView)

        var closeButtom = ImageButton(this)
        linearLayoutParams = LinearLayout.LayoutParams(40, 40)
        linearLayoutParams.gravity = Gravity.CENTER
        linearLayoutParams.marginEnd = 40
        closeButtom.layoutParams = linearLayoutParams
        closeButtom.background  = resources.getDrawable(R.mipmap.close,null)
        closeButtom.setOnClickListener {
            linearLayout_translate_contant.removeView(itenLayout)
        }
        itenLayout.addView(closeButtom)

        linearLayout_translate_contant.addView(itenLayout)
    }

    fun count() {
        var amount: Float? = null
        var rate: Float? = null
        try {
            amount = edittext_mony_amount.text.toString().toFloat()
            rate = edittext_mony_rate.text.toString().toFloat()
        } catch (e: Exception) {
            amount = null
            rate = null
        }
        //
        for (index in 0 until linearLayout_translate_contant.childCount) {
            var itemLayout: LinearLayout =
                linearLayout_translate_contant.getChildAt(index) as LinearLayout
            var inputView: EditText = itemLayout.getChildAt(1) as EditText
            var resultView: TextView = itemLayout.getChildAt(2) as TextView

            try {
                if (amount == null || rate == null) {
                    resultView.text = "持有数据解析失败"
                } else {
                    var inputRate = inputView.text.toString().toFloat()
                    var result = 0f
                    if(handleType){
                        result = amount*inputRate -  amount*rate
                    }else{
                        result = amount/inputRate -  amount/rate
                    }
                    var name = when(type02){
                        0 ->{
                            "Piso"
                        }
                        1->{
                            "Yuan"
                        }
                        2->{
                            "Dollar"
                        }
                        else -> "null"
                    }
                    if(result>0){
                        resultView.text = "盈:${DecimalFormat("#.00").format(Math.abs(result))}"+name
                        resultView.setTextColor(Color.RED)
                    }else if(result<0){
                        resultView.text = "亏:${DecimalFormat("#.00").format(Math.abs(result))}"+name
                        resultView.setTextColor(Color.GREEN)
                    }else{
                        resultView.text = "持平"
                        resultView.setTextColor(Color.BLACK)
                    }
                }
            } catch (e: Exception) {
                resultView.text = "交换数据解析失败"
            }
        }
    }

    fun refreshType(){
        if(type01>type02){
            handleType = true
        }else{
            handleType = false
        }
    }

    var checkChangeListener =object: RadioGroup.OnCheckedChangeListener{
        override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
            when(checkedId){
                R.id.radioButton_type_01 ->{
                    type01 = 0
                }
                R.id.radioButton_type_02 ->{
                    type01 = 1
                }
                R.id.radioButton_type_03 ->{
                    type01 = 2
                }
                R.id.radioButton_type_011 ->{
                    type02 = 0
                }
                R.id.radioButton_type_022 ->{
                    type02 = 1
                }
                R.id.radioButton_type_033 ->{
                    type02 = 2
                }
            }
            refreshType()
        }
    }

    var onClickListener = object : View.OnClickListener {
        override fun onClick(v: View?) {
            when (v) {
                button_add -> {
                    addItem()
                }
                button_count -> {
                    count()
                }
            }
        }
    }


}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="持有币种">
            </TextView>

            <RadioGroup
                android:id="@+id/radioGroup_type"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <RadioButton
                    android:id="@+id/radioButton_type_01"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:text="Piso"
                    android:checked="true">
                </RadioButton>

                <RadioButton
                    android:id="@+id/radioButton_type_02"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:text="Yuan"
                    >
                </RadioButton>

                <RadioButton
                    android:id="@+id/radioButton_type_03"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:text="Dollar"
                    >

                </RadioButton>
            </RadioGroup>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="交换币种">
            </TextView>

            <RadioGroup
                android:id="@+id/radioGroup_type02"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <RadioButton
                    android:id="@+id/radioButton_type_011"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:text="Piso"
                    >
                </RadioButton>

                <RadioButton
                    android:id="@+id/radioButton_type_022"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:text="Yuan"
                    android:checked="true"
                    >
                </RadioButton>

                <RadioButton
                    android:id="@+id/radioButton_type_033"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:text="Dollar"
                    >

                </RadioButton>
            </RadioGroup>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="持有数量">
                </TextView>
                <EditText
                    android:id="@+id/edittext_mony_amount"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"></EditText>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="持有汇率">
                </TextView>
                <EditText
                    android:id="@+id/edittext_mony_rate"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"></EditText>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/linearLayout_translate_contant"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
            </LinearLayout>

            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/button_add"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="添加交换汇率">
            </androidx.appcompat.widget.AppCompatButton>

            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/button_count"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="计算换汇">
            </androidx.appcompat.widget.AppCompatButton>
        </LinearLayout>
    </ScrollView>




</LinearLayout>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值