如何在按下DONE键盘时不关闭键盘
问题描述:
当用户在软键盘上按下“完成”时,键盘将关闭。我希望它只在特定条件成立时才会关闭(例如密码输入正确)。如何在按下DONE键盘时不关闭键盘
这是我的代码(设置当按下“完成”按钮,以便监听器):
final EditText et = (EditText)findViewById(R.id.et);
et.setOnEditorActionListener(new OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if(actionId==EditorInfo.IME_ACTION_DONE)
{
if (et.getText().toString().equals(password)) // they entered correct
{
// log them in
}
else
{
// bring up the keyboard
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Toast.makeText(Main.this, "Incorrect.", Toast.LENGTH_SHORT).show();
}
}
return false;
}
});
我知道这不工作的原因可能是因为它之前运行这段代码它实际上自行关闭软键盘,但这就是为什么我需要帮助。我不知道另一种方式。
答案可能的话题可以与合作:
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
,诸如此类的事情,但我不知道。
SOLUTION:
EditText et = (EditText)findViewById(R.id.et);
et.setOnEditorActionListener(new OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if(actionId==EditorInfo.IME_ACTION_DONE)
{
if (et.getText().toString().equals(password)) // they entered correct
{
// log them in
return false; // close the keyboard
}
else
{
Toast.makeText(Main.this, "Incorrect.", Toast.LENGTH_SHORT).show();
return true; // keep the keyboard up
}
}
// if you don't have the return statements in the if structure above, you
// could put return true; here to always keep the keyboard up when the "DONE"
// action is pressed. But with the return statements above, it doesn't matter
return false; // or return true
}
});
答
如果您onEditorAction
方法你的回报true
,动作不会被再次处理。在这种情况下,当动作为EditorInfo.IME_ACTION_DONE
时,您可以返回true
以不隐藏键盘。
很好的答案。我找不到任何有关该方法应该返回的文档。 –