Android 将焦点从一个编辑文本框移到另一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1796671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Move focus from one Edit text box to another
提问by Vamsi
I am writing a simple calculator application(using absolute layout with 3 edit boxes and some buttons), which has two inputtext boxes and a output box.
我正在编写一个简单的计算器应用程序(使用带有 3 个编辑框和一些按钮的绝对布局),它有两个输入文本框和一个输出框。
input1 = (EditText) findViewById(R.id.input1);
input2 = (EditText) findViewById(R.id.input2);
now once user enters some numerics into input1 and presses '+', now i want to shift the focus from input1 to input2. How can i do that?
现在一旦用户在 input1 中输入一些数字并按“+”,现在我想将焦点从 input1 转移到 input2。我怎样才能做到这一点?
I tried the below code on keypress of '+'
我在“+”的按键上尝试了以下代码
onClick(View arg0){
operator.setText("+");
//Move focus from input1 to input2
input1.clearFocus();
input2.setNextFocusDownId(input2.getId());
}
but this is not working.. can you please help me on this?
但这不起作用..你能帮我吗?
回答by Vamsi
Well, I found the answer: we can simply call input2.requestFocus();
to change the focus.
好吧,我找到了答案:我们可以简单地打电话input2.requestFocus();
来改变焦点。
回答by danigonlinea
Try this:
尝试这个:
input1.setNextFocusDownId(input2.getId());
input1.setNextFocusDownId(input2.getId());
you are using input2, and I guess that you want to go from input1 to input2.
您正在使用 input2,我猜您想从 input1 转到 input2。
回答by Eric Mill
[By the way, you want to avoid using AbsoluteLayout - it's deprecated, and may be removed.]
[顺便说一句,您想避免使用 AbsoluteLayout - 它已被弃用,并且可能会被删除。]
Instead of using onClick, you want the action to happen in a KeyListener's onKeyDown method.
您希望操作发生在 KeyListener 的 onKeyDown 方法中,而不是使用 onClick。
See: setKeyListener
请参阅:setKeyListener
Then you can examine the KeyEvent's key with
然后你可以检查KeyEvent的键
KeyEvent.getAction() == KeyEvent.KEYCODE_PLUS