2012-09-14 2 views
6

Voglio spostare il cursore da EditText1 a un altro EditText2. Mi ero già concentrato su editText1 ma su come spostare il cursore su editText2.?Android Spostare il cursore da un EditText a un altro se si fa clic su una lettera qualsiasi nel campo?

+0

forse aggiungere un [TextWatcher] (http://developer.android.com/reference/android/text/TextWatcher.html) sul tuo 'EditText1' – Aprian

+0

vedere la mia risposta qui: http: //stackoverflow.com/questions/9003166/android-keyboard-next-button-issue-on-edittext/9003285#9003285 Potrebbe essere d'aiuto. – Hiral

+0

Grazie a entrambi (Aprian e Hiral) per l'aiuto. Ora funziona, TextWatcher mi aiuta ..... – anoop

risposta

0

Impostare le proprietà nel codice click edittext1 ...

EditText2.requestFocus();

0
EditText editText1 = (EditText)findViewById(R.id.editText1); 
    EditText editText2 = (EditText)findViewById(R.id.editText2); 


editText1.setOnKeyListener(new 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 
      editText1.clearFocus(); 
      editText2.requestFocus(); 
      return true; 
     } 
     return false; 
} 
}); 
10

Finaly ho avuto la risposta:

editText1.addTextChangedListener(new TextWatcher() { 

       public void onTextChanged(CharSequence s, int start, int before, 
         int count) { 
        Integer textlength1 = editText1.getText().length(); 

        if (textlength1 >= 1) { 
         editText2.requestFocus(); 
        } 
       } 

       @Override 
       public void afterTextChanged(Editable s) { 
        // TODO Auto-generated method stub 
       } 

       @Override 
       public void beforeTextChanged(CharSequence s, int start, int count, 
         int after) { 
        // TODO Auto-generated method stub 
       } 
      }); 

      editText2.addTextChangedListener(new TextWatcher() { 

       public void onTextChanged(CharSequence s, int start, int before, 
         int count) { 
        Integer textlength2 = editText1.getText().length(); 

        if (textlength2 >= 1) { 
         editText3.requestFocus(); 

        } 
       } 

       @Override 
       public void afterTextChanged(Editable s) { 
        // TODO Auto-generated method stub 
       } 

       @Override 
       public void beforeTextChanged(CharSequence s, int start, int count, 
         int after) { 
        // TODO Auto-generated method stub 

       } 
      }); 
2

posso capire la vostra risposta,

Ma c'è un altro buon modo per farlo semplicemente utilizzando il seguente attributo

android: imeOptions = "actionNext"

L'esempio:

<EditText 
android:hint="@string/hint_user_name" 
android:id="@+id/et_user_name" 
android:maxLines="2" 
style="@style/EditText_Login" 
android:imeOptions="actionNext" 
/> 

Grazie,