2012-09-14 60 views
6

我想将光标从EditText1移动到另一个EditText2。我已经专注于编辑Text1,但是如何将光标移动到editText2。在edittext1点击代码)Android如果点击字段中的任何字母,将光标从一个EditText移动到另一个EditText?

+0

也许添加[TextWatcher(http://developer.android.com/reference/android/text/TextWatcher.html)在你的'EditText1' – Aprian

+0

在这里看到我的答案:http://stackoverflow.com/questions/9003166/android-keyboard-next-button-issue-on-edittext/9003285#9003285它可能会有所帮助。 – Hiral

+0

感谢你(Aprian和Hiral)的帮助。 现在它的工作,TextWatcher帮助我..... – anoop

回答

0

设置属性...

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我得到了答案:

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

我能理解你的答案,

但是有做它的另一个好办法简单地通过以下属性

android:imeOptions =“actionNext”

的例子:

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

感谢,

相关问题