Umesh
Umesh

Reputation: 86

Cursor not focus next edittext

This my code please check it and help me find out this problem. Basically move cursor from one edit-text to another one if click any letter in field. if you have any better way for this then please help me.I was trying this yesterday.but i not got any solution.actually after second edit-text cursor still and not move next one.I was doing this another way but not success.Thanks in advance.

package osvin.com.edittext;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity implements  TextWatcher{

     EditText edt_1,edt_2,edt_3,edt_4;
     StringBuilder sb;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intialize();
        edt_1.addTextChangedListener(this);
        edt_2.addTextChangedListener(this);
        edt_3.addTextChangedListener(this);
        edt_4.addTextChangedListener(this);
     }
     public void Intialize(){
        edt_1=(EditText)findViewById(R.id.edt_1);
        edt_2=(EditText)findViewById(R.id.edt_2);
        edt_3=(EditText)findViewById(R.id.edt_3);
        edt_4=(EditText)findViewById(R.id.edt_4);
        sb=new StringBuilder();
     }

     @Override
     public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        if(sb.length()==1)
        {
           sb.deleteCharAt(0);

        }
     }

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

        if (sb.length() == 0&&edt_1.length() == 1) {

            sb.append(s);
            Log.e("StringBuilderLength", "--------" + sb.length());
                edt_1.clearFocus();
                edt_2.requestFocus();
                edt_2.setCursorVisible(true);

        }
        if (sb.length() == 0&&edt_2.length() == 1) {
            sb.append(s);
            Log.e("StringBuilderLength", "--------2" + sb.length());
                edt_2.clearFocus();
                edt_3.requestFocus();
                edt_3.setCursorVisible(true);

        }
        if (sb.length() ==0&&edt_3.length() == 1) {
            sb.append(s);
            Log.e("StringBuilderLength", "--------3" + sb.length());
                edt_3.clearFocus();
                edt_4.requestFocus();
                edt_4.setCursorVisible(true);
            }
            if (sb.length() ==0&&edt_4.length() == 1) {
                sb.append(s);
                Log.e("StringBuilderLength", "--------4" + sb.length());
                edt_4.requestFocus();
                edt_4.setCursorVisible(true);
            }

     }

     @Override
     public void afterTextChanged(Editable s) {


     }


}

Upvotes: 1

Views: 218

Answers (1)

Rashin
Rashin

Reputation: 806

The main problem was with your if's logic. Try with this:

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


    if (edt_1.getText().toString().equals(s.toString())) {
        Log.e("StringBuilderLength", "--------");
        edt_1.clearFocus();
        edt_2.requestFocus();
        edt_2.setCursorVisible(true);

    }
    if (edt_2.getText().toString().equals(s.toString())) {
        Log.e("StringBuilderLength", "--------2");
        edt_2.clearFocus();
        edt_3.requestFocus();
        edt_3.setCursorVisible(true);

    }
    if (edt_3.getText().toString().equals(s.toString())) {
        Log.e("StringBuilderLength", "--------3");
        edt_3.clearFocus();
        edt_4.requestFocus();
        edt_4.setCursorVisible(true);
    }
    if (edt_4.getText().toString().equals(s.toString())) {
        Log.e("StringBuilderLength", "--------4");
        edt_4.requestFocus();
        edt_4.setCursorVisible(true);
    }

}

Upvotes: 1

Related Questions