繁体   English   中英

保持软键盘在Toast下-ANDROID

[英]keep soft keyboard under the Toast - ANDROID

我的活动将EditText中包含的用户名保存到数据库,还显示错误消息,例如“用户名已存在”,并调用某些方法。

如果我通过单击接受按钮(在我的按钮中为Onclick侦听器)来保存用户名,Toast将显示在SoftKeyboard上方,但是如果我通过按Enter键(对Onkey方法进行编程)来保存用户名,则softkeyboard会消失并显示Toast。

我希望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;
  }

}

您只需单击按钮即可显示软键盘

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

然后烘烤任何短信,它将起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM