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

Asked

Viewed 1,544 times

2

I am 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"
        />

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

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

It follows below 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) {

        }
    });

    }
}

I thank you for your help ! hugs

  • 1

    You have to initialize the attributes letra1_1 and letra1_2, in the onCreate(), using the method findViewById()

  • 1

    Thank you very much ! now it worked ! hugs

3 answers

3


Try this, put in the after for being the action shoots after releasing the click, but you can do in other ways or put a test for 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) {}
});
  • How does that make the input move on to the next Edittext?

  • places letra2.requestFocus(); inside the afterTextChanged() event. I think it would be if I understood what you need.

  • Yes it is, but you didn’t put in the answer.

  • Sorry, I forgot the requestFocus method()

  • Thank you ! I had no idea how to do this rsrs, but I put this snippet and the app stopped working when I put it to run ... can I update the question with the java code so you can see if I’m doing something wrong? I’m new here sorry whatever ...

  • 1

    @Linkonlouvison Yes, enter the code and the log error

Show 1 more comment

1

Let’s say the user is editing the letra1 field and you want to, after editing, position the cursor on the letra2, you can do it like this:

letra2.requestFocus();
  • This only solves part of the problem, missing explain how to run it automatically.

0

trial :

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 its.xml file layout where Voce has its Edittext use the attribute

 android:maxLength="1"
  • sorry for the last line there , I had not seen it in your layout.xml file

Browser other questions tagged

You are not signed in. Login or sign up in order to post.