Enable button event by entering an Edit

Asked

Viewed 904 times

1

I created a "register" screen where the user informs the name/nickname and where there is a Continue button that goes to another screen. I would like this new screen to open when the user presses the Enter key, but I do not know how to do for the Enter key to have this function.

Below the code of the home screen

public class CadastroActivity extends AppCompatActivity {

EditText edtNome;

Button btnContinuar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cadastro);

    edtNome = (EditText) findViewById(R.id.edtNome);

    btnContinuar = (Button) findViewById(R.id.btnContinuar);

    btnContinuar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent EnviarDados = new Intent(CadastroActivity.this, ResultadoActivity.class);

            String txtNome = edtNome.getText().toString();

            Bundle bundle = new Bundle();

            bundle.putString("nome", txtNome);

            EnviarDados.putExtras(bundle);

            startActivity(EnviarDados);

        }
    });
}

}

1 answer

3


The best practice to achieve this is to use the event setOnEditorActionListener of EditText as shown this answer or this one here.

You will need to configure Edittext in XML (if not done) with the imeOptions corresponding to "next field".

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:imeOptions="actionNext" />

Note that here we set the property maxLines="1", that will make Android know that when pressing Enter, you must advance the field. The android:imeOptions="actionNext" changes the design of the "Enter" button with an arrow, indicating to the user that by clicking there it will be taken to the next field/screen/etc. Set this property in XML also sets the identification of the Edit event so that you can execute the action you want.

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_NEXT) {
            callAnotherActivity();
            return true;
        }
        return false;
    }
});
  • 1

    That would be my suggestion. I would not be able to go so deep for technical difficulties of my own, not to have deepened in Android, but by the rumor theory suggest it. And important detail: in this answer, implicitly the action of calling the other activity was encapsulated in a method. So this call can be leveraged to be used by the button as well

  • Yes, as a way of learning I put the explicit Software. But I could have done editText.setOnEditorActionListener(this), Extend the Activity interface OnEditorActionListener and make a field check to be validated with v.equals(editText).

Browser other questions tagged

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