简体   繁体   中英

Editext and RadioButton in android

I have two EditText boxes and a RadioGroup which contains two radio buttons. When I start entering input in any of edittext boxes, All the radiobuttons has to be checked off (if any one of buttons has checked already) automatically and another edittext also should be cleared. And If i check any of two radio buttons then all edittext boxes should be cleared(if they have any inputted data). Here is what i have done so far.

Inside oncreate() :

    et1 = (EditText)findViewById(R.id.editText1);
    et1.setOnFocusChangeListener(this);
    et1.addTextChangedListener(this);

    et2 = (EditText)findViewById(R.id.editText2);
    et2.setOnFocusChangeListener(this);
    et2.addTextChangedListener(this); 

    radioButton1 = (RadioButton)findViewById(R.id.rb1);
    radioButton2 = (RadioButton)findViewById(R.id.rb2);
    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);        
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
        public void onCheckedChanged(RadioGroup group, int checkedId) {

             et1.setText("");
            et2.setText("");

        }

    });

And ...

 @Override
public void onFocusChange(View v, boolean hasFocus) {

    switch (v.getId()) {
    case R.id.editText1:
        whoHasFocus = 1;

        break;
    case R.id.editText2 :
        whoHasFocus = 2;

        break;

    }

}

And

   @Override
public void afterTextChanged(Editable edt) {

    if(whoHasFocus == 1){
        String enteredName1 = edt.toString().trim();

        if(enteredName1.length() == 1){
            if(et2.getText().toString().trim().length() >= 1)
                et2.setText("");

            radioButton1.setChecked(false);
            radioButton2.setChecked(false);

        } 

    }else if(whoHasFocus == 2){

        String enteredName2 = edt.toString().trim();
        if(enteredName2.length() == 1){
            if(et1.getText().toString().trim().length() >= 1)
                et1.setText("");

            radioButton1.setChecked(false);
            radioButton2.setChecked(false);
        } 
    }
}

When i check any radio button and start typing in edittext, radiobutton is getting unchecked but nothing is entering into edittext for the first time. If i enter next letter then it is entering the second letter and working fine then onwards. But First letter is not being entered into endittext when a radiobutton is checked. please help me. Where iam going wrong?

Solved

            radioGroup.setOnCheckedChangeListener(null);
            radioButton1.setChecked(false);
            radioButton2.setChecked(false);
            radioGroup.setOnCheckedChangeListener(this);

And

    et1.removeTextChangedListener(MyActivity.this);
    et2.removeTextChangedListener(MyActivity.this);
    et1.setText(""); 
    et2.setText("");
    et1.addTextChangedListener(MyActivity.this);
    et2.addTextChangedListener(MyActivity.this);

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