Android 问题:EditText、KeyListener 和物理后退按钮
我的活动实现了 KeyListener,我的 edittext 有一个关键侦听器集。
editor = new EditText(this);
editor.setMinLines(4);
editor.setMinimumWidth(400);
editor.setKeyListener(this);
当用户键入内容并按软键盘上的“Enter”键时,文本视图的文本将设置为用户输入。
@Override
public int getInputType() {
return InputType.TYPE_TEXT_FLAG_MULTI_LINE;
}
@Override
public boolean onKeyDown(View view, Editable text, int keyCode,
KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_ENTER){
outview.setText(editor.getText());
}
return true;
}
这里的outview是一个TextView。我的问题是,在此活动中,物理后退按钮不起作用。按下它,什么也没有发生。任何建议都会受到欢迎。
My activity implements KeyListener and my edittext has a key listener set.
editor = new EditText(this);
editor.setMinLines(4);
editor.setMinimumWidth(400);
editor.setKeyListener(this);
WHen the user types something and presses "enter" on the softkeyboard a textview's text is set to the users input.
@Override
public int getInputType() {
return InputType.TYPE_TEXT_FLAG_MULTI_LINE;
}
@Override
public boolean onKeyDown(View view, Editable text, int keyCode,
KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_ENTER){
outview.setText(editor.getText());
}
return true;
}
Here outview is a TextView. My problem is that in this activity the physical back button doesn't work. Press it and nothing happens. ANy advice would be welcomed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过从
onKeyDown
函数返回true
,您将通知 Android 您已处理所有按键事件。相反,仅在输入键的情况下返回 true。否则返回false
。这将允许 Android 处理后退按钮按键。By returning
true
from theonKeyDown
function, you are informing Android that you have handled all key events. Instead, only return true in the case of the enter key. Returnfalse
otherwise. This will allow Android to handle the back button key press.