Mar*_*ian 27 android android-softkeyboard
当我的用户按下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!");
}
Run Code Online (Sandbox Code Playgroud)
......在这里我的观点
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content">
<EditText android:id="@+id/studentEntrySalary" android:text="Foo" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我的虚拟设备上可能有问题?
jqp*_*liq 68
这应该这样做:
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;
}
});
Run Code Online (Sandbox Code Playgroud)
小智 18
保持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();
}
}
Run Code Online (Sandbox Code Playgroud)
如果您将文本框设置为单行(我相信在布局xml文件中将其称为SingleLine),它将在输入时退出键盘.
你去了:http://developer.android.com/reference/android/R.styleable.html#TextView_singleLine
| 归档时间: |
|
| 查看次数: |
37468 次 |
| 最近记录: |