Android:将键盘隐藏在一个被覆盖的;“完成”;编辑文本按键
我使用了一些Android代码来覆盖EditText字段中的“完成”按钮:Android:将键盘隐藏在一个被覆盖的;“完成”;编辑文本按键,android,overriding,android-edittext,Android,Overriding,Android Edittext,我使用了一些Android代码来覆盖EditText字段中的“完成”按钮: myEditField.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == Edi
myEditField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
mySubroutine();
return true;
}
return false;
}
});
激活该字段将调用键盘,按“完成”将成功计算mySubroutine()。但是,当我按“完成”时,键盘不再消失。如何将此默认行为恢复到例程?您可以通过以下操作关闭键盘:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
为什么不:
myEditField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
mySubroutine();
}
return false;
}
});
处理代码后,只需返回false。这可以解释为无论您的代码(mySubroutine())做什么,它都会在以后使用默认操作。如果返回“true”,则表示您是一个快乐的编码者,需要完成的所有事情都发生在mySubroutine()中,默认操作不需要执行。您必须将onClickListener附加到执行以下代码的按钮上:
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editview.getWindowToken(), 0);
(new Handler()).postDelayed(new Runnable() {
public void run() { yourEditText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
yourEditText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
}
}, 200);
我也有同样的问题。在editText可见性从“消失”更改为“可见”后,我必须立即设置焦点并显示软键盘。我使用以下代码实现了这一点:
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editview.getWindowToken(), 0);
(new Handler()).postDelayed(new Runnable() {
public void run() { yourEditText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
yourEditText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
}
}, 200);
我在使用Eclipse时遇到了以下两个错误:“InputMethodManager无法解析为类型”和“对于类型new TextView.onEditorActionListener(){}的方法getWindowToken()未定义”我不是Java程序员,因此不知道如何解释这些错误。我是否需要包含更早的内容?请尝试按Control+Shift+o进行相应的导入。谢谢,我需要一个依赖项。我还必须将getWindowToken()替换为v.getWindowToken(),但在其他方面效果很好,谢谢!现在我只需要理解它做了什么,这是正确的方法,否则您将与框架的设计方式背道而驰。我已经在一个相关的问题中详细阐述了这一点,我已经四年没有接触过Android代码了,所以我相信你的话。