0

I have a form with two editext and condition is only one should be enable when we enter value to any of edittext e.g We have 2 edittexts ...Edittext1 and Editext2 case 1: When we type edittext1 then editext2 should be disable and greyout. Case 2 : when we type editext2 the editext1 should be disable and greyout. enter image description here

I had tried using textwatcher but couldn't find the logic.

TextWatcher cupToDup = 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) {
        String cupTOdup = et_cup_to_dup.getText().toString().trim();
        et_dup_to_cup.setFocusable(false);
        //et_dup_to_cup.setText(" ");
        et_dup_to_cup.setEnabled(false);

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};

TextWatcher dupTocup = 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) {

        String dupTocup = et_dup_to_cup.getText().toString().trim();
        et_cup_to_dup.setFocusable(false);
        //et_cup_to_dup.setText(" ");
        et_cup_to_dup.setEnabled(false);
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};
  • 2
    What do you want to happen if the user has entered text into one `EditText` but changes their mind and wants to use the other? How do you 'unlock' it? – codebod May 31 '22 at 15:35
  • Use [OnKeyUp and OnKeyDown](https://developer.android.com/training/keyboard-input/commands#:~:text=%2Dscreen%20keyboard) to determine whether the other `EditText` shoud be enabled or disabled. – Alias Cartellano May 31 '22 at 17:37
  • @AliasCartellano - Appreciate ur help, but it will work if we disable editext? – MKE SIRIES II Jun 01 '22 at 01:49
  • @codebod - there are two editextexts and we need only one is the core logic. – MKE SIRIES II Jun 01 '22 at 01:50
  • I understand that the contents of only one `EditText` is needed for subsequent processing. I'm trying to understand the user experience you are trying to achieve. – codebod Jun 01 '22 at 09:35
  • @codebod - In image i have posted there are two edittexts... when user enter edittext then second should be greyout(visible) and when we tap second editext it should be again back to normal and first editext will be greyout. Hope you got me now – MKE SIRIES II Jun 01 '22 at 10:20

1 Answers1

0

I found the solution like this,

  et_dup_to_cup.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                    et_cup_to_dup.setText("");
                    et_dup_to_cup.setAlpha(1);
                    et_cup_to_dup.setAlpha(0.25f);
                   
                }
            }
        });

        et_cup_to_dup.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                    et_dup_to_cup.setText("");
                    et_cup_to_dup.setAlpha(1);
                    et_dup_to_cup.setAlpha(0.25f);
                   
                }
            }
        });