Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
5.9 kB
2
Indexable
Never
package com.example.program1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText editTextName, editTextSurname, editTextGrades;
    private Button buttonGrades;


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

        editTextName = findViewById(R.id.editTextName);
        editTextSurname = findViewById(R.id.editTextSurname);
        editTextGrades = findViewById(R.id.editNumberOfGrades);
        buttonGrades = findViewById(R.id.buttonGrades);

        editTextName.addTextChangedListener(namechange);
        editTextSurname.addTextChangedListener(surnamechange);
        editTextGrades.addTextChangedListener(gradeschange);

        editTextName.addTextChangedListener(buttonvisible);
        editTextSurname.addTextChangedListener(buttonvisible);
        editTextGrades.addTextChangedListener(buttonvisible);

        editTextName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                String name = editTextName.getText().toString().trim();
                if(!b && name.isEmpty()){
                    Toast.makeText(MainActivity.this, getString(R.string.name_statement), Toast.LENGTH_SHORT).show();
                    editTextName.setError(getString(R.string.name_statement));
                }
            }
        });

        editTextSurname.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                String surname = editTextSurname.getText().toString().trim();
                if(!b && surname.isEmpty()){
                    Toast.makeText(MainActivity.this, getString(R.string.surname_statement), Toast.LENGTH_SHORT).show();
                    editTextSurname.setError(getString(R.string.surname_statement));
                }
            }
        });

        editTextGrades.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                String numberOfGrades = editTextGrades.getText().toString().trim();

                if(!b && (numberOfGrades.isEmpty() || Integer.parseInt(numberOfGrades) < 5 || Integer.parseInt(numberOfGrades) > 15)){
                    Toast.makeText(MainActivity.this, getString(R.string.grades_statement), Toast.LENGTH_SHORT).show();
                    editTextGrades.setError(getString(R.string.grades_statement));
                }
            }
        });

    }

    public TextWatcher namechange = 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) {
            if(charSequence.length() == 0){
                editTextName.setError(getString(R.string.name_statement));
                buttonGrades.setVisibility(View.INVISIBLE);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };

    public TextWatcher surnamechange = 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) {
            if(charSequence.length() == 0){
                editTextSurname.setError(getString(R.string.surname_statement));
                buttonGrades.setVisibility(View.INVISIBLE);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };

    public TextWatcher gradeschange = 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) {
            if(charSequence.length() == 0 || Integer.parseInt(charSequence.toString()) < 5 || Integer.parseInt(charSequence.toString()) > 15){
                editTextGrades.setError(getString(R.string.grades_statement));
                buttonGrades.setVisibility(View.INVISIBLE);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };

    public TextWatcher buttonvisible = 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) {
            String name = editTextName.getText().toString().trim();
            String surname = editTextSurname.getText().toString().trim();
            String numberOfGrades = editTextGrades.getText().toString().trim();

            if (!name.isEmpty() && !surname.isEmpty() && !numberOfGrades.isEmpty()
                    && Integer.parseInt(numberOfGrades) >= 5 && Integer.parseInt(numberOfGrades) <= 15){
                buttonGrades.setVisibility(View.VISIBLE);
            } else{
                buttonGrades.setVisibility(View.INVISIBLE);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };

}