简体   繁体   中英

Writing inside EditText

I have two EdiText and when I write a number in the first one I would need the second to be set to 162 - first one. If it is necessary to re-type a second number, the component should recalculate the first number.

If I write something in the second the first must behave exactly like the second.

Below there is my code but it does not work:

    inputScoreWe = findViewById(R.id.inputScoreWe);
    inputScoreYou = findViewById(R.id.inputScoreYou);

    View.OnClickListener inputScoreListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {

                int inputScoreWeInteger = Integer.parseInt(inputScoreWe.getText().toString());
                int inputScoreYouInteger = Integer.parseInt(inputScoreYou.getText().toString());

                if (inputScoreWeInteger > 0) {
                    inputScoreYouInteger = 162 - inputScoreWeInteger;
                } else if (inputScoreYouInteger > 0) {
                    inputScoreWeInteger = 162 - inputScoreYouInteger;
                }

                String s1 = inputScoreWeInteger + "";
                String s2 = inputScoreYouInteger + "";

                inputScoreWe.setText(s1);
                inputScoreYou.setText(s2);

            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();

            }
        }
    };

    inputScoreWe.setOnClickListener(inputScoreListener);
    inputScoreYou.setOnClickListener(inputScoreListener);

Use text change listener to trigger for everytime you change value

 inputScoreWe.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                   if(s.length() > 0){
                      inputScoreYou.setText(162 - Integer.parseInt(inputScoreYou.getText().toString())+"");
                   }

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });

replace your code with code as below

        inputScoreWe = findViewById(R.id.inputScoreWe);
        inputScoreYou = findViewById(R.id.inputScoreYou);


        inputScoreWe.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    inputScoreYou.setText(162 - Integer.parseInt(inputScoreYou.getText().toString()) + "");
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        inputScoreYou.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    inputScoreWe.setText(162 - Integer.parseInt(inputScoreWe.getText().toString()) + "");
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

try this

brandET = findViewById(R.id.addCar_brand);
        modelET = findViewById(R.id.addCar_model);

        brandET.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                brandChange = hasFocus;
            }
        });

        modelET.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                modelChange = hasFocus;
            }
        });


        brandET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if (brandChange && count > 0) {

                    int dataFromBrand = Integer.parseInt(s.toString());
                    modelET.setText((162 - dataFromBrand) + "");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        modelET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if (modelChange && count > 0) {

                    int dataFromModel = Integer.parseInt(s.toString());
                    brandET.setText((162 - dataFromModel) + "");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

here brandET and modelET is your two edit text...and...brandChange and modelChange are two global boolean data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM