gpt4 book ai didi

android在按下回车键时设置隐藏键盘(在EditText中)

转载 作者:IT老高 更新时间:2023-10-28 22:00:25 26 4
gpt4 key购买 nike

当我的用户在虚拟 android 上按 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!");
}

...这里是我的观点

 <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>

我的虚拟设备可能有问题?

最佳答案

应该这样做:

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;
}
});

关于android在按下回车键时设置隐藏键盘(在EditText中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2434532/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com