2013-06-25 17 views
0

我的活动将包含在EditText中的用户名保存到数据库中,同时显示错误消息,例如“用户名已存在”,调用某些方法。保持烤面包下的软键盘--ANDROID

如果我通过单击一个接受按钮(我的按钮中的Onclick监听器)保存用户名称Toast在SoftKeyboard上方显示,但是如果通过按Enter键保存用户名(编程方法Onkey)softkeyboard消失并且吐司出现。

我希望sofkeyboard不会消失以显示吐司。

这里是我的代码:

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; 
    } 

} 

回答

0

你可以只显示在按钮

InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE); 
imm.showSoftInput(ed, 0); 

的点击softkeyboard然后举杯任何文字信息,它会工作。

+0

我没有onclick方法的问题,问题是与Onkey –

+0

你可以使用onKey – blganesh101

+0

中的同一段代码,你可以看到代码是exaclty相同的,我不知道是什么问题,在onKey或一个foccus返回声明,我不知道。 我认为我的onkey代码是错误的 –