我的活动将 EditText 中包含的用户名保存到数据库,还显示错误消息,例如“用户名已存在”,调用了一些方法。
如果我通过单击接受按钮(我的按钮中的 Onclick 侦听器)保存用户名,则 Toast 将显示在 SoftKeyboard 上方,但如果我通过按 Enter 键(编程 Onkey 方法)保存用户名,则软键盘消失并出现 Toast。
我希望软键盘不会消失以显示吐司。
这是我的代码:
public class CrearUsuario extends Activity implements View.OnClickListener, View.OnKeyListener {
EditText nombreUsuario;
String user;
Button flecha;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.crearusuario_layout);
nombreUsuario = (EditText)findViewById(R.id.etNombreUsuario);
flecha = (Button) findViewById(R.id.btFlechaDerecha);
flecha.setOnClickListener(this);
nombreUsuario.setOnKeyListener(this);
}
@Override
public void onClick(View v) {
//Convertimos el contenido en la caja de texto en un String
user = nombreUsuario.getText().toString().trim();
//Si el tamaño del String es igual a 0, que es es lo mismo que dijeramos "Si esta vacio"
if (user.length() == 0) {
//Creamos el aviso
//codigo de Toast centrado REUTILIZABLE!!
Toast toast = Toast.makeText(this, "Para comenzar introduce un nombre de usuario", Toast.LENGTH_SHORT);
TextView nepe = (TextView)toast.getView().findViewById(android.R.id.message);
if( nepe != null) nepe.setGravity(Gravity.CENTER);
toast.show();
} else {
//Conectamos con la base de datos
//Creamos un bojeto y lo iniciamos con new
AdaptadorBD db = new AdaptadorBD(this);
db.abrir();
//creamos un String que ocntenga todos los nombres de usuario para comprobar si no hay duplicados
String duplicado = db.obtenerNombresDeUsuario();
if (duplicado.contains(user)){
//Creamos el aviso
//codigo de Toast centrado REUTILIZABLE!!
Toast toast = Toast.makeText(this, "El nombre de usuario ya existe, por favor selecciona un nombre de usuario distinto", Toast.LENGTH_SHORT);
TextView nepe = (TextView)toast.getView().findViewById(android.R.id.message);
if( nepe != null) nepe.setGravity(Gravity.CENTER);
toast.show();
}else{
//insertamos el nombre de usuario en la base de datos
db.insertarContacto(user, "0");
db.cerrar();
//inicia la siguiente activity
Intent intent = new Intent("MENU");
startActivity(intent);
finish();
}
}
}
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
switch(i){
case KeyEvent.KEYCODE_ENTER:
//Convertimos el contenido en la caja de texto en un String
user = nombreUsuario.getText().toString().trim();
//Si el tamaño del String es igual a 0, que es es lo mismo que dijeramos "Si esta vacio"
if (user.length() == 0) {
//codigo de Toast centrado REUTILIZABLE!!
Toast toast = Toast.makeText(this, "Para comenzar introduce un nombre de usuario", Toast.LENGTH_SHORT);
TextView nepe = (TextView)toast.getView().findViewById(android.R.id.message);
if( nepe != null) nepe.setGravity(Gravity.CENTER);
toast.show();
}
else {
//Conectamos con la base de datos
//Creamos un bojeto y lo iniciamos con new
AdaptadorBD db = new AdaptadorBD(this);
db.abrir();
String duplicado = db.obtenerNombresDeUsuario();
if (duplicado.contains(user)){
//Creamos el aviso
Toast toast = Toast.makeText(this, "El nombre de usuario ya existe, por favor selecciona un nombre de usuario distinto", Toast.LENGTH_SHORT);
TextView nepe = (TextView)toast.getView().findViewById(android.R.id.message);
if( nepe != null) nepe.setGravity(Gravity.CENTER);
toast.show();
}else{
//insertamos el nombre de usuario en la base de datos
db.insertarContacto(user, "0");
db.cerrar();
//inicia la siguiente activity
Intent intent = new Intent("MENU");
startActivity(intent);
finish();
}
}
return true;
}
}return false;
}
}