当我的用户按下Enter虚拟机器人"用户验证条目!"时 键盘我的键盘保持可见!(为什么?)
这是我的Java代码......
private void initTextField() { entryUser = (EditText) findViewById(R.id.studentEntrySalary); entryUser.setOnKeyListener(new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN) { switch (keyCode) { case KeyEvent.KEYCODE_DPAD_CENTER: case KeyEvent.KEYCODE_ENTER: userValidateEntry(); return true; } } return true; } }); } private void userValidateEntry() { System.out.println("user validate entry!"); }
......在这里我的观点
我的虚拟设备上可能有问题?
这应该这样做:
yourEditTextHere.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) { InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); // NOTE: In the author's example, he uses an identifier // called searchBar. If setting this code on your EditText // then use v.getWindowToken() as a reference to your // EditText is passed into this callback as a TextView in.hideSoftInputFromWindow(searchBar .getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); userValidateEntry(); // Must return true here to consume event return true; } return false; } });
保持singleLine ="true"并将imeOptions ="actionDone"添加到EditText.然后在OnEditorActionListener中检查actionId == EditorInfo.IME_ACTION_DONE,如此(但将其更改为您的实现):
if (actionId == EditorInfo.IME_ACTION_DONE) { if ((username.getText().toString().length() > 0) && (password.getText().toString().length() > 0)) { // Perform action on key press InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(username.getWindowToken(), 0); doLogin(); } }
如果您将文本框设置为单行(我相信在布局xml文件中将其称为SingleLine),它将在输入时退出键盘.
你去了:http://developer.android.com/reference/android/R.styleable.html#TextView_singleLine