0

This code not working , what's Wrong? I don't want to allow user input until radiobutton2 is checked

EditText t4 = (EditText)findViewById(R.id.editText3);

 RadioButton  rb = (RadioButton) findViewById(R.id.radioButton1);
 RadioButton rb2 = (RadioButton) findViewById(R.id.radioButton2);
 RadioGroup  rg =(RadioGroup) findViewById(R.id.G1);


    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            if(checkedId==R.id.radioButton1)
            {
                t4.setEnabled(false);
            }

            if(checkedId==R.id.radioButton2)
            {
                t4.setEnabled(true);
            }


        }
    });
Charuක
  • 12,953
  • 5
  • 50
  • 88

6 Answers6

2
t4.setEnable(rb2.isChecked());

rb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                t4.setEnable(isChecked);
            }
        });
Pratik Popat
  • 2,891
  • 20
  • 31
1

Can you try the code given below?

EditText t4 = (EditText)findViewById(R.id.editText3);

RadioButton  rb = (RadioButton) findViewById(R.id.radioButton1);
RadioButton rb2 = (RadioButton) findViewById(R.id.radioButton2);
RadioGroup  rg =(RadioGroup) findViewById(R.id.G1);


rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        if(checkedId==R.id.radioButton1)
        {
            changeEditTextAvailability(t4, false);
        }

        if(checkedId==R.id.radioButton2)
        {
            changeEditTextAvailability(t4, true);
        }


    }
});

private void changeEditTextAvailability(EditText editText, boolean status) {
    editText.setFocusable(status);
    editText.setEnabled(status);
    editText.setCursorVisible(status);
    editText.setFocusableInTouchMode(status)
}

Cheers,

Renc

0

You can try this

Check if “Radiobutton” is checked: isChecked()

Disable EditText in android: setFocusable()

EditText t4 = (EditText)findViewById(R.id.editText3);

RadioButton  rb = (RadioButton) findViewById(R.id.radioButton1);
RadioButton rb2 = (RadioButton) findViewById(R.id.radioButton2);
RadioGroup  rg =(RadioGroup) findViewById(R.id.G1);

if (rb.isChecked()) {
   t4.setFocusable(true);
} else {
   t4.setFocusable(false);
}

if (rb2.isChecked()) {
   t4.setFocusable(false);
} else {
   t4.setFocusable(true);
}
Donald Wu
  • 698
  • 7
  • 20
0
    if (cbProhibitEditPW.isChecked()) { // disable editing password
       editTextPassword.setFocusable(false);
       editTextPassword.setFocusableInTouchMode(false); // user touches widget on phone with touch screen
       editTextPassword.setClickable(false); // user navigates with wheel and selects widget
} else { // enable editing of password
       editTextPassword.setFocusable(true);
       editTextPassword.setFocusableInTouchMode(true);
       editTextPassword.setClickable(true);
}

Try this code

Zaki Pathan
  • 1,762
  • 1
  • 13
  • 26
0

The problem is that you have not initialized the validations after initializing the views Try this in on onCreate()

EditText t4 = (EditText)findViewById(R.id.editText3);

RadioButton  rb = (RadioButton) findViewById(R.id.radioButton1);
RadioButton rb2 = (RadioButton) findViewById(R.id.radioButton2);
RadioGroup  rg =(RadioGroup) findViewById(R.id.G1);

if (rb.isChecked()) {
   t4.setFocusable(true);
}

if (rb2.isChecked()) {
   t4.setFocusable(false);
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            if(checkedId==R.id.radioButton1)
            {
                t4.setEnabled(false);
            }

            if(checkedId==R.id.radioButton2)
            {
                t4.setEnabled(true);
            }


        }
    });

This will restrict user to input when screen launches

Quick learner
  • 10,632
  • 4
  • 45
  • 55
0

A workaround is to call setFocusable(false) in addition to setEnabled(false) on the EditText. Change your code as shown below:

    EditText t4 = (EditText)findViewById(R.id.editText3);

 RadioButton  rb = (RadioButton) findViewById(R.id.radioButton1);
 RadioButton rb2 = (RadioButton) findViewById(R.id.radioButton2);
 RadioGroup  rg =(RadioGroup) findViewById(R.id.G1);


    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            if(checkedId==R.id.radioButton1)
            {
                t4.setEnabled(false);
                t4.setFocusable(false);
            }

            if(checkedId==R.id.radioButton2)
            {
                t4.setEnabled(true);
                t4.setFocusable(true);

            }


        }
    });
Vicky
  • 1,807
  • 5
  • 23
  • 34