Silvio Pereira Silvio Pereira - 5 months ago 39
Android Question

Press Enter to change EditText

My problem is that I have a login screen that contains 2 EditText views. When I enter the username and click Enter, it adds more lines, but I want it to change to the EditText password instead. Can someone please help me?

public void onClick(View v){

final TextView user = (TextView)findViewById(R.id.tnome);
final TextView pw = (TextView)findViewById(R.id.tpw);
if (user.getText().toString().equals("") || pw.getText().toString().equals((""))) {
Toast.makeText(MainActivity.this, "Têm de Preencher todos os Campos", Toast.LENGTH_SHORT).show();
user.requestFocus();

}else{
if (user.getText().toString().endsWith(" ")){
user.setText(user.getText().toString().substring(0,user.length()-1));
}
user.setText(user.getText().toString().toLowerCase());
if (user.getText().toString().equals("admin") && pw.getText().toString().equals("admin")){

startActivity(new Intent(MainActivity.this,Home.class));
user.setText("");
pw.setText("");
}
user.setOnKeyListener(new View.OnKeyListener() {

public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER))
{
// Perform action on Enter key press
user.clearFocus();
pw.requestFocus();
return true;
}
return false;
}
});

pw.setOnKeyListener(new View.OnKeyListener() {

public boolean onKey(View v, int keyCode, KeyEvent event) {

if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER))
{
// Perform action on Enter key press
// check for username - password correctness here
return true;
}
return false;
}
});
}
}

Answer

Using the android:inputType="text" (or another value) to limit input text to one line.

Usually, when input controls are laid out in a layout file (resx), pressing ENTER will cause focus to go to the next field. You shouldn't need any custom handling code to have this work (i.e. you shouldn't need code on the username field to make ENTER set focus to the password field).

You can use the <requestFocus /> tag to force focus to a field when the page is loaded.

<EditText
    android:id="@+id/etUserName"
    android:layout_margin="5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textCapWords"
    android:focusable="true"
    >
    <requestFocus />
</EditText>


<EditText
    android:id="@+id/etPassword"
    android:layout_margin="5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:cursorVisible="true"
    android:hint="Password"
    />

The most likely reason that the wire-up of the listener isn't working is that the code is only run when a button is clicked (it's in an onClick function). Perform the wire-up for the password (pw) field during page initialization instead - onFinishInflate() is a good spot.

You might, however want to wire-up a key listener to make an ENTER press in the password field submit the data (or do validation).

Comments