I want to create a converter

1

I have this code with which I want to make a converter, in this I have 3 EditText and what I want is to be able to enter data in celsius and that automatically converts me farenheit and kelvin, and I also want to be able to enter data in farenheit and that automatically turn the celsius and the kelvin, and likewise for the kelvin.

In case what I want to do is that EditText can enter data and also show data at the same time. I appreciate your support, I'm still a beginner.

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:text="Conversor de temperatura"
    android:textSize="30dp"
    android:textAlignment="center"
    android:layout_margin="10dp"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="celsius"
    android:textAlignment="center"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="fahrenheit"
    android:textAlignment="center"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="kelvin" 
    android:textAlignment="center"/>

    
asked by Moises Perez 17.06.2018 в 06:05
source

1 answer

0

first of all you have to start giving them their respective IDs to each EditText after this, you have to start configuring your activity in java, which is in app-> Java- > NombreDelPaquete being there, you will find a file where you will enter all the code, in the method you find and it is called onCreate as a suggestion you can create a method in this case I will call conversor in which we will define how each of your EditText

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


void conversor(){
    final EditText editText1 =findViewById(R.id.Edit_Publicacion);//en lugar de Edit_Publicaciónn ingresas el ID de su respectivo EditText
    final EditText editText2 =findViewById(R.id.Edit_Publicacion);//en lugar de Edit_Publicaciónn ingresas el ID de su respectivo EditText
    final EditText editText3 =findViewById(R.id.Edit_Publicacion);//en lugar de Edit_Publicaciónn ingresas el ID de su respectivo EditText

    editText1.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            //despues de que finalice de editar el texto obtenemos el valor de cada EditText y lo modificamos
            //pasarlo de celsius a la conversión que tu quieras, aquí usas tus formulas
            editText2.setText(Integer.parseInt(editText1.getText().toString())*200/1000);
            editText3.setText(Integer.parseInt(editText1.getText().toString())*200/1000);
        }
    });

    editText2.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            //despues de que finalice de editar el texto obtenemos el valor de cada EditText y lo modificamos
            //pasarlo de celsius a la conversión que tu quieras, aquí usas tus formulas
            editText1.setText(Integer.parseInt(editText2.getText().toString())*200/1000);
            editText3.setText(Integer.parseInt(editText2.getText().toString())*200/1000);
        }
    });
    editText3.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            //despues de que finalice de editar el texto obtenemos el valor de cada EditText y lo modificamos
            //pasarlo de celsius a la conversión que tu quieras, aquí usas tus formulas
            editText2.setText(Integer.parseInt(editText3.getText().toString())*200/1000);
            editText1.setText(Integer.parseInt(editText3.getText().toString())*200/1000);
        }
    });


}

They are repeated 3 times because you have to have 3 events for each EditText for each one

Source that can help you understand the operation of addTextChangedListener

    
answered by 17.06.2018 в 07:01