I'm making a program to calculate taxes, and I want to make the numbers typed in editText be separated in real time with decimal places (commas). Separate in decimal places both the numbers to be typed in the editText and the result that will be obtained after the calculations. Thanks!!!
Here is the code that does the respective calculations. KOTLIN...
package com.marianomuendane.contabiliadadeVocacional
import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
import java.util.*
class IRPCFragment : Fragment() {
//variables for the respective calculations
private lateinit var proveitos: EditText
private lateinit var custos: EditText
private lateinit var amortizacoes: EditText
private lateinit var juros: EditText
private lateinit var format: String
private lateinit var decimal: DecimalFormat
private lateinit var decimalSymbols: DecimalFormatSymbols
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_i_r_p_c, container, false)
proveitos = view.findViewById(R.id.proveitos)
proveitos.setRawInputType(Configuration.KEYBOARD_12KEY)
//decimal number attempt
//proveitos.addTextChangedListener(editText1())
decimalSymbols = DecimalFormatSymbols(Locale("pt", "Brazil"))
decimalSymbols.setDecimalSeparator(',')
decimalSymbols.setGroupingSeparator('.')
format = "###,###,###,###.####"
decimal = DecimalFormat(format)
// EditText necessarios
//==========================================================
custos = view.findViewById<EditText>(R.id.custos)
custos.setRawInputType(Configuration.KEYBOARD_12KEY)
//==========================================================
amortizacoes = view.findViewById<EditText>(R.id.amortizacoes)
amortizacoes.setRawInputType(Configuration.KEYBOARD_12KEY)
//==========================================================
juros = view.findViewById(R.id.juros)
juros.setRawInputType(Configuration.KEYBOARD_12KEY)
//==========================================================
val imposto = view.findViewById<TextView>(R.id.imposto)
imposto.setTextIsSelectable(true)
val bt4 = view.findViewById<Button>(R.id.res_final)
val resolucao = view.findViewById<Button>(R.id.resolucao_Irpc)
//======================================================================================
//function of calculations
fun iRPC() =
if (proveitos.text.isEmpty() && custos.text.isEmpty() && amortizacoes.text.isEmpty()
&& juros.text.isEmpty()
) {
Toast.makeText(requireContext(), "Preencha os valores ", Toast.LENGTH_SHORT).show()
} else if (proveitos.text.isEmpty()) {
Toast.makeText(requireContext(), "Preencha proveitos", Toast.LENGTH_SHORT).show()
} else if (custos.text.isEmpty()) {
Toast.makeText(requireContext(), "Preencha custos", Toast.LENGTH_SHORT).show()
} else if (amortizacoes.text.isEmpty()) {
Toast.makeText(requireContext(), "Preencha amortizacoes", Toast.LENGTH_SHORT).show()
} else if (juros.text.isEmpty()) {
Toast.makeText(requireContext(), "Preencha juros", Toast.LENGTH_SHORT).show()
} else {
val prov = proveitos.text.toString().toFloat()
val cust = custos.text.toString().toFloat()
val amort = amortizacoes.text.toString().toFloat()
val jur = juros.text.toString().toFloat()
val result = (prov - cust - amort - jur) * 0.32
val formatResult = decimal.format(result)
imposto.setText(result.toString())
}
//======================================================================================
bt4.setOnClickListener {
iRPC()
}
return view
}
}