java关闭键盘,java – 如果按下键盘上的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() {

那种事情,但我不知道肯定.

解:

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

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值