Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KeyCode_Enter to next edittext

In edittext, after typing 'Enter' key, system make a new line inside it. I'd like to focus on next edittext, no new line. how to code? my code in xml is below

    <EditText     android:id="@+id/txtNPCode"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:textSize="18sp"     android:layout_alignLeft="@+id/lblNPCode"     android:layout_below="@+id/lblNPCode"     android:layout_centerHorizontal="true"/> <EditText     android:id="@+id/txtCNPCode"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:textSize="18sp"     android:layout_alignLeft="@+id/lblCNPCode"     android:layout_below="@+id/lblCNPCode"     android:layout_centerHorizontal="true"/>   

I also caputer key code in setOnKeyListener

   tCNPCode.setOnKeyListener(new OnKeyListener() {         public boolean onKey(View v, int keyCode, KeyEvent event) {             // TODO Auto-generated method stub             if(keyCode == 66) {                 Toast.makeText(S_PCode.this, "Enter Key", Toast.LENGTH_LONG).show();                 //tNPCode.setFocusable(true);             }             return false;         }     }); 
like image 964
soclose Avatar asked Jun 11 '10 03:06

soclose


2 Answers

You don't even have to use OnKeyListener. Simply add line android:singleLine="true" to your EditText. After this operation EditText behaves exactly like you want. So, in your particular case:

<EditText     android:id="@+id/txtNPCode"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:textSize="18sp"     android:layout_alignLeft="@+id/lblNPCode"     android:layout_below="@+id/lblNPCode"     android:layout_centerHorizontal="true"     android:singleLine="true" /> <EditText     android:id="@+id/txtCNPCode"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:textSize="18sp"     android:layout_alignLeft="@+id/lblCNPCode"     android:layout_below="@+id/lblCNPCode"     android:layout_centerHorizontal="true" /> 

solves the problem.

like image 82
Antek Avatar answered Sep 21 '22 19:09

Antek


You have to use the requestFocus method. In your case it will be something like:

tCNPCode.setOnKeyListener(new OnKeyListener() {     public boolean onKey(View v, int keyCode, KeyEvent event) {         if(keyCode == KeyEvent.KEYCODE_ENTER) {             txtCNPCode.requestFocus();         }         return false;     } }); 
like image 44
Cristian Avatar answered Sep 20 '22 19:09

Cristian