Quando l'utente preme "Fine" sulla tastiera software, la tastiera si chiude. Lo voglio in modo che si chiuda solo se una certa condizione è vera (ad esempio la password è stata inserita correttamente).Come NON chiudere la tastiera quando viene premuto DONE sulla tastiera
Questo è il mio codice (istituisce un listener per quando si preme il pulsante "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;
}
});
mi rendo conto che la ragione per cui questo non funziona è probabilmente perché si corre questo codice prima chiude effettivamente la tastiera virtuale da sola, ma è per questo che ho bisogno di aiuto. Non conosco un altro modo.
Una possibile argomento di risposte potrebbe essere al lavoro con:
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
e quel genere di cose, ma non so per certo.
SOLUZIONE:
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
}
});
Ottima risposta. Non sono riuscito a trovare alcuna documentazione su cosa il metodo dovrebbe restituire. –