I've already searched how to do this but couldn't find any solution. Here it goes, I have edittext1 and edittext2, while the carat/cursor is positioned in edittext1 then I pressed "Next/Enter" key in soft keyboard, the carat/cursor must be positioned in edittext2. The snippet below received the event when I pressed the "Next/Enter" key but didn't move the carat/cursor in edittext2.
edittext1.setOnKeyListener(new View.OnKeyListener()
{
@Overri开发者_运维知识库de
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
{
Editable e = edittext2.getText();
Selection.setSelection(e,e.length());
}
return false;
}
});
Any inputs will be highly appreciated. Thanks.
Rather than doing it in code, can't you use the android:nextFocusDown
and android:nextFocusUp
in your XML? Here are some references:
- Handling UI Events
- View documentation
EDIT
With your accepted answer it looks like you have a solution that works, however I thought I'd get the XML route to work too. So here is a working version of your layout elements:
<AutoCompleteTextView
android:id="@+id/autoCompleteTextViewRecipient"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:completionThreshold="1"
android:inputType="text"
android:maxLines="1"
android:hint="To"
android:imeOptions="actionNext"
android:nextFocusDown="@+id/editTextComposeMessage"
android:nextFocusUp="@+id/editTextComposeMessage"/>
<EditText
android:id="@+id/editTextComposeMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:nextFocusDown="@+id/autoCompleteTextViewRecipient"
android:nextFocusUp="@+id/autoCompleteTextViewRecipient"/>
Differences are that the AutoCompleteTextView
now has a nextFocusDown
, the EditText
has a nextFocusUp
, and both have imeOptions
set to actionNext
Try using this:
if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
{
edittext2.requestFocus();
}
The cursor moves to edittext2.
精彩评论