Java – how to disable closing the keyboard if you press done on the keyboard
•
Java
When the user presses "finish" on the soft keyboard, the keyboard closes I want it to be turned off only when certain conditions are true (for example, the password is entered correctly)
This is my code (set a listener when the finish button is pressed):
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;
}
});
I realized that the reason why this didn't work might be that it actually turned off its own soft keyboard before it ran this code, but that's why I need help I don't know another way
One possible answer may be:
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
That kind of thing, but I don't know for sure
Solution:
EditText et = (EditText)findViewById(R.id.et);
et.setOnEditorActionListener(new OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v,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,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
}
});
Solution
If you return true from your oneditoraction method, the action will not be processed again In this case, when the operation is editorinfo IME_ ACTION_ When done, you can return true to not hide the keyboard
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
