Mic*_*ski 15 java android android-softkeyboard
当用户按下软键盘上的"完成"时,键盘将关闭.我希望它只有在某个条件为真时才关闭(例如,密码输入正确).
这是我的代码(为按下"完成"按钮时设置一个监听器):
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;
}
});
Run Code Online (Sandbox Code Playgroud)
我意识到这不起作用的原因可能是因为它在实际关闭软键盘之前运行此代码,但这就是我需要帮助的原因.我不知道另一种方式.
可能的答案主题可能是:
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
Run Code Online (Sandbox Code Playgroud)
那种事,但我不确定.
解:
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
}
});
Run Code Online (Sandbox Code Playgroud)
Dev*_*rim 22
如果true从您的onEditorAction方法返回,则不会再次处理操作.在这种情况下,您可以true在操作时返回不隐藏键盘EditorInfo.IME_ACTION_DONE.
| 归档时间: |
|
| 查看次数: |
4930 次 |
| 最近记录: |