【问题标题】:keep soft keyboard under the Toast - ANDROID在 Toast 下保留软键盘 - ANDROID
【发布时间】:2013-06-25 09:32:16
【问题描述】:

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

如果我通过单击接受按钮(我的按钮中的 Onclick 侦听器)保存用户名,则 Toast 将显示在 SoftKeyboard 上方,但如果我通过按 Enter 键(编程 Onkey 方法)保存用户名,则软键盘消失并出现 Toast .

我希望软键盘不消失以显示 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;
  }

}

【问题讨论】:

    标签: android onclick android-softkeyboard toast onkeydown


    【解决方案1】:

    你可以在点击按钮时显示软键盘

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

    然后祝酒任何短信,它会工作。

    【讨论】:

    • 我的onclick方法没问题,问题出在Onkey上
    • 如您所见,代码完全一样,我不知道是什么问题,onKey 中的返回语句或焦点,我不知道。我认为我的 onkey 代码是错误的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    相关资源
    最近更新 更多