将克拉/光标从 edittext1 设置为 edittext2
我已经搜索过如何执行此操作但找不到任何解决方案。在这里,我有edittext1和edittext2,而克拉/光标位于edittext1中,然后我按软键盘中的“Next/Enter”键,克拉/光标必须位于edittext2中。当我按下“Next/Enter”键但没有移动 edittext2 中的克拉/光标时,下面的代码片段收到了事件。
edittext1.setOnKeyListener(new View.OnKeyListener()
{
@Override
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;
}
});
任何意见都将受到高度赞赏。谢谢。
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()
{
@Override
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能使用
android:nextFocusDown
和android:nextFocusUp
?以下是一些参考:编辑
根据您接受的答案,看起来您有一个可行的解决方案,但是我认为我也可以让 XML 路线发挥作用。因此,这是布局元素的工作版本:
区别在于
AutoCompleteTextView
现在有一个nextFocusDown
,EditText
有一个nextFocusUp
,并且都将imeOptions
设置为actionNext
Rather than doing it in code, can't you use the
android:nextFocusDown
andandroid:nextFocusUp
in your XML? Here are some references: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:
Differences are that the
AutoCompleteTextView
now has anextFocusDown
, theEditText
has anextFocusUp
, and both haveimeOptions
set toactionNext
尝试使用这个:
if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
{
edittext2.requestFocus();
光标
移动到edittext2。
Try using this:
if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
{
edittext2.requestFocus();
}
The cursor moves to edittext2.