Mask of entry of measures in metro

Asked

Viewed 66 times

0

I am developing an Btus Air Conditioning Calculation App, which simply receives units of measurements and recommends an air conditioning capacity suitable for the environment. The problem is I want the user to be able to separate the meters from the centimeter without having to manually enter the "." for this, that it was made automatic according to the typing as it is done with the monetary mask.

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

    public class MainActivity extends AppCompatActivity {

    private EditText editLargura;
    private EditText editComprimento;
    private TextView textResultado;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editLargura = findViewById(R.id.editLargura);
        editComprimento = findViewById(R.id.editComprimento);
        textResultado = findViewById(R.id.textResultado);

    }

    public  void realizarCalculo (View view){

        String largura = findViewById(R.id.editLargura).toString();
        String comprimento = findViewById(R.id.editComprimento).toString();

        Boolean camposValidados = this.validarCampos (largura, comprimento);
        if (camposValidados){

            this.calcularMelhorPotencia(largura, comprimento);

        }else{
            textResultado.setText("Preencha os campos primeiro" +
                    " e caso não tenha pessoas ou eletrônicos digite 0 nesses campos");
        }

    }

    public void calcularMelhorPotencia(String mLargura, String mComprimento){
        Double medidasLargura = Double.parseDouble(mLargura);
        Double medidasComprimento = Double.parseDouble(mComprimento);

        Double resultado = medidasLargura * medidasComprimento;

        if( resultado >= 3000 && resultado  <= 7000 ){
            textResultado.setText("Para esse ambiente terá que instalar um ar condicionado de 7.000 BTU´s");
        }else if( resultado >= 7001 && resultado <= 9000 ) {
            textResultado.setText("Para esse ambiente terá que instalar um ar condicionado de 9.000 BTU´s");
        }else if( resultado >= 9001 && resultado <= 12000 ){
            textResultado.setText("Para esse ambiente terá que instalar um ar condicionado de 12.000 BTU´s");
        }else if( resultado >= 12001 && resultado <= 18000 ){
            textResultado.setText("Para esse ambiente terá que instalar um ar condicionado de 18.000 BTU´s");
        }else if( resultado >= 18001 && resultado <= 22000 ){
            textResultado.setText("Para esse ambiente terá que instalar um ar condicionado de 22.000 BTU´s");
        }else if( resultado >= 22001 && resultado <= 30000 ){
            textResultado.setText("Para esse ambiente terá que instalar um ar condicionado de 30.000 BTU´s");
        }else if( resultado >= 30001 && resultado <= 36000 ){
            textResultado.setText("Para esse ambiente terá que instalar um ar condicionado de 36.000 BTU´s");
        }else if( resultado >= 36001 && resultado <= 48000 ){
            textResultado.setText("Para esse ambiente terá que instalar um ar condicionado de 48.000 BTU´s");
        }else if( resultado >= 48001 && resultado <= 60000 ){
            textResultado.setText("Para esse ambiente terá que instalar um ar condicionado de 60.000 BTU´s");
        }else if( resultado >= 60001 ){
            textResultado.setText("Será necessário entrar em contato com a Okay Ar Condicionado para criação de um projeto de climatização");
        }
    }
    public Boolean validarCampos(String mLargura, String mComprimento){
        Boolean camposValidados = true;
        if (mLargura==null || mComprimento.equals("")){
            camposValidados = false;
        }else if (mLargura==null || mComprimento.equals("")){
            camposValidados = false;
        }

        return camposValidados;
    }

}

And that’s the xml layout

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView
    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">
<androidx.constraintlayout.widget.ConstraintLayout

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editPessoas"
        android:layout_width="138dp"
        android:layout_height="43dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="ex: 2"
        android:inputType="textPersonName"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView4" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        android:text="digite a largura"
        android:textColor="#000000"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />

    <EditText
        android:id="@+id/editLargura"
        android:layout_width="138dp"
        android:layout_height="43dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="ex: 3.25"
        android:inputType="number|numberDecimal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        android:text="digite o comprimento"
        android:textColor="#000000"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="Saiba qual a potência de ar condicionado compra"
        android:textAlignment="center"
        android:textColor="#000000"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editComprimento"
        android:layout_width="138dp"
        android:layout_height="43dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="ex: 4.12"
        android:inputType="numberDecimal"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="205dp" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        android:text="digite quantas pessoas"
        android:textColor="#000000"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editLargura" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        android:text="digite quantos eletrônicos"
        android:textColor="#000000"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toBottomOf="@+id/editComprimento" />

    <EditText
        android:id="@+id/editEletronicos"
        android:layout_width="138dp"
        android:layout_height="43dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="ex: 3"
        android:inputType="textPersonName"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toBottomOf="@+id/textView5" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="8dp"
        android:background="@color/colorPrimaryDark"
        android:onClick="realizarCalculo"
        android:text="verificar"
        android:textColor="#FFFFFF"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editEletronicos" />

    <TextView
        android:id="@+id/textResultado"
        android:layout_width="356dp"
        android:layout_height="144dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="8dp"
        android:text="Clique no botão após preencher os campos"
        android:textAlignment="center"
        android:textColor="@android:color/system_notification_accent_color"
        android:textSize="22sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

1 answer

0

Good morning, my friend.... there are two ways to do this: 1 Through code or in the second way that is much simpler and I’ll show you below:

** 1st Implementation **

  • Add the following line to your build.Radle

implementation 'com.github.santalu:mask-edittext:1.0.2

** 2º Adding to xml **

  • Replace the Edittext that will receive the entry with <com.santalu.maskedittext.MaskEditText that is, see the example below:

    < com.santalu.maskedittext.Maskedittext android:id="@+id/editPeople" android:layout_width="138dp" android:layout_height="43dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:Ems="10" android:hint="ex: 2" android:inputType="textPersonName" app:layout_constraintStart_toStartOf="Parent" app:layout_constraintTop_toBottomOf="@+id/textView4" app:met_mask="#. ## m" />

Where app:met_mask="#.## m" is the mask you want to apply. For each # is equivalent to one digit per user.

** 3º Recover the entered value **

  • You can recover the entered value in 2 different ways:

The value with the mask:

String aString = maskText.getRawText().trim();

  • Ex output: 1.35 m

The value without the mask:

String aString = maskText.getText().toString().trim();

  • Former of output.: 135
  • good afternoon friend! Thanks for the extreme help, this already works as I wanted and I can follow up on my project, I just wanted to know if there is any method that allows you to move the point forward as you type more numbers or are limited to the model that is declared in xml as app:met_mask="#. ##" and if you typed more numbers switch to app:met_mask="#. ##" and dot remained fixed before the last two digits.

  • Voce wants to do as in the applications of banks is this?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.