How do I go to the next field (an EditText) without having to press Next on android?

2

I'm doing a crossword and each field is an EditText as in the example below:

// GRIDLAYOUT USADO PARA ORGANIZAR OS EDIT TEXT EM UMA MATRIZ
<GridLayout
    android:layout_width="match_parent"
    android:layout_height="382dp"
    android:layout_weight="0.26">

     <EditText
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:gravity="center"
        android:id="@+id/letra1_1"
        android:inputType="textCapSentences"
        android:maxLength="1"
        android:singleLine="true"
        android:textSize="25dp"
        android:background="@drawable/square_semborda"
        android:layout_row="1"
        android:layout_column="1"
        />

    <EditText
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:gravity="center"
        android:id="@+id/letra1_2"
        android:inputType="textCapSentences"
        android:maxLength="1"
        android:singleLine="true"
        android:textSize="25dp"
        android:background="@drawable/square_semborda"
        android:layout_row="1"
        android:layout_column="2"
        />

I would like to jump to the next field automatically when the user places a character in the field it is in.

I tried to use the android:nextFocus attribute but it asks enter to jump to another field.

The following is the java code as it was ... but the app stops working when I access the screen of this activity

package com.example.android.cruzadinhas_eic;

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

public class LeptospiroseActivity extends AppCompatActivity {

public EditText letra1_1;
public EditText letra1_2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_leptospirose);





    letra1_1.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {
            letra1_2.requestFocus();
        }

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


        }

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

        }
    });

    }
}

Thank you for your help! hugs

    
asked by anonymous 06.06.2016 / 14:26

3 answers

3

Try this, I put it in the after because the action fires after releasing the click, but you can do in other methods or put a test for a certain letter.

yourEditText.addTextChangedListener(new TextWatcher() {

    public void afterTextChanged(Editable s) {
           otherEditText.requestFocus();
    }

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

    public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
    
06.06.2016 / 14:39
1

Let's say that the user is editing the letter1 field and you want to position the cursor on letter2 after editing, you can do it like this:

letra2.requestFocus();
    
06.06.2016 / 14:43
0

try:

public class MainActivity extends AppCompatActivity
{
    public EditText edLetterOne,edLetterTwo;

    @Override
    protected void onCreate(Bundle b)
    {
        super.onCreate(b);
        setContentView(R.layout.activity_main);

        edLetterOne = (EditText)findViewById(R.id.editText);
        edLetterTwo = (EditText)findViewById(R.id.editText2);

        edLetterOne.addTextChangedListener(textWatcher);
    }
    /////////////////////////////////////
    //////////// TextWatcher ////////////
    /////////////////////////////////////
    TextWatcher textWatcher = new TextWatcher()
    {
        @Override
        public void beforeTextChanged(CharSequence c, int i, int i1, int i2){}

        @Override
        public void onTextChanged(CharSequence c, int start, int before, int count)
        {
            if(count > 0) edLetterTwo.requestFocus();
        }

        @Override
        public void afterTextChanged(Editable editable){}
    };
}

and in your layout.xml file where you have your EditText use the

 android:maxLength="1"
    
04.08.2016 / 11:51