androidandroid-edittextfocusfocusable

Two EditText One has to be completed before pass to the other


First of all sorry if I have mistakes on my english.

I'm quite new to android. My problem here is that I have two EditText, call them Name and Phone, on the activity. So What I want is that the user can't write down in Phone if before, the EditText Name wasn't completed.

I have tried implementing the OnFocusChange method but I don't know if I'm implementing it well.

So there is my class code, where "et_nom" is the EditText Name and "et_tel" is the EditText Phone. What I'm doing wrong?

Thank You

public class MainActivity extends Activity implements OnClickListener, OnFocusChangeListener{

    EditText et_nom, et_tel;
    RadioButton rg_mascle,rg_femella;
    Switch sw_carnet;
    RatingBar rb_estrellas;
    String texto;
    Button b_enviar;
    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.primera);
        et_nom = (EditText)findViewById(R.id.et_nom);
        et_tel = (EditText)findViewById(R.id.et_tel);
        rg_mascle = (RadioButton)findViewById(R.id.rg_mascle);
        rg_femella = (RadioButton)findViewById(R.id.rg_femella);
        sw_carnet = (Switch)findViewById(R.id.sw_carnet);
        rb_estrellas = (RatingBar)findViewById(R.id.rb_estrellas);
        b_enviar = (Button)findViewById(R.id.b_enviar1);
        b_enviar.setOnClickListener(this);
        et_tel.setFocusable(false);
        et_tel.setClickable(true);

    }

    public void onRadioButtonClicked(View v)
    {
        boolean checked = ((RadioButton) v).isChecked();

    }

    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.b_enviar1)
        {
            intent = new Intent(MainActivity.this, Segunda_Act.class);
            startActivity(intent);
        }

    }

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

        if(v.getId() == R.id.et_nom && hasFocus)
        {
            et_tel.setFocusable(true);
        }

    }


}

Solution

  • You may want to look at implementing a text change watcher instead, and enable or disable the second EditText based on the contents of the first:

        editText1.addTextChangedListener(new TextWatcher() {
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void afterTextChanged(Editable s) {
                if(editText1.getText() != null && !editText1.getText().equals(""))
                {
                    editText2.setEnabled(true);
                }
                else
                {
                    editText2.setEnabled(false);
                }
            }
        });
    

    This will enable the second edit text if there is any text in the first, and disable the second if the first is empty.