【问题标题】:Why the toast is not activated by pressing the button in android?为什么在android中按下按钮没有激活吐司?
【发布时间】:2014-01-24 11:02:57
【问题描述】:

在以下条件下,当您单击“boton_continuar”按钮时,我正在尝试显示祝酒词: EditExt Vacuum (numero_celular) 和 ckeckbox (check) 检查号。这是我的代码

boton_continuar.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        //Verificamos que se halla clikeado y se halla ingresado
        if(check.isChecked() && numero_celular.getText().length() != 0){

            Intent intent = new Intent(RegisterActivity.this, PrincipalActivity.class);
            startActivity(intent);
        }else{
            Context contexto = getApplicationContext();
            if(!check.isChecked()){                 
                Toast.makeText(contexto, "Debe aceptar los términos", 2000);
            }
            if(numero_celular.getText().length() == 0){
                Toast.makeText(contexto, "Debe ingresar su número", 2000);
            }
    }
}});

【问题讨论】:

    标签: android android-edittext android-button android-toast


    【解决方案1】:

    敬酒后请务必致电.show()

    boton_continuar.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            //Verificamos que se halla clikeado y se halla ingresado
            if(check.isChecked() && numero_celular.getText().length() != 0){
                Intent intent = new Intent(RegisterActivity.this, PrincipalActivity.class);
                startActivity(intent);
            } else {
                Context contexto = getApplicationContext();
                if(!check.isChecked()){                 
                    Toast.makeText(contexto, "Debe aceptar los términos", 2000).show();
                }
                if(numero_celular.getText().length() == 0){
                    Toast.makeText(contexto, "Debe ingresar su número", 2000).show();
                }
            }
        }});
    

    【讨论】:

    • 还要注意“2000”不是makeText(...)的有效第二个参数
    • "2000" 在技术上是有效的第二个参数,但不推荐。它只会默认为 Toast.LENGTH_SHORT(巧合的是,它是 2000 毫秒)。
    【解决方案2】:

    两件事:

    1. 在您制作的 Toast 上致电 .show()
    2. 2000 不是 Toast 的有效参数。这不会让它显示 2000 毫秒。 Toast 接受的唯一参数是 Toast.LENGTH_SHORTToast.LENGTH_LONG

    所以:

    更改这些行:

    Toast.makeText(contexto, "Debe aceptar los términos", 2000);
    Toast.makeText(contexto, "Debe ingresar su número", 2000);
    

    到这些行:

    Toast.makeText(contexto, "Debe aceptar los términos", Toast.LENGTH_SHORT).show();
    Toast.makeText(contexto, "Debe ingresar su número", Toast.LENGTH_SHORT).show();
    

    Toast.LENGTH_SHORT 是 2000 毫秒(2 秒),Toast.LENGTH_LONG 是 3500 毫秒(3.5 秒)。

    • Toast.LENGTH_SHORT == 0 所以你可以通过 0 代替。
    • Toast.LENGTH_LONG == 1 所以你可以通过 1 代替。

    请访问Toast 上的 Android 文档以获取详细信息。

    【讨论】:

      【解决方案3】:

      你必须使用 SHOW 函数。

      做:

      Toast.makeText(contexto, "Debe ingresar su número", 2000).show();
      

      【讨论】:

        【解决方案4】:

        您需要在“Toast.makeText()”之后添加“.show()”。

        例如:

        if(!check.isChecked()){                 
                    Toast.makeText(contexto, "Debe aceptar los términos", 2000).show();
                    }
                    if(numero_celular.getText().length() == 0){
                        Toast.makeText(contexto, "Debe ingresar su número", 2000).show();
                    }
        

        【讨论】:

          【解决方案5】:

          尝试以下方法:

           boton_continuar.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View arg0) {
                  //Verificamos que se halla clikeado y se halla ingresado
                  if(check.isChecked() && numero_celular.getText().length() != 0){
          
          
                      Intent intent = new Intent(RegisterActivity.this, PrincipalActivity.class);
                      startActivity(intent);
                  }else{
                      Context contexto = getApplicationContext();
                      if(!check.isChecked()){                 
                          Toast.makeText(contexto, "Debe aceptar los términos", 2000).show();
                      }
                      if(numero_celular.getText().length() == 0){
                          Toast.makeText(contexto, "Debe ingresar su número", 2000).show();
                      }
          
          
                  }
          
              }});
          

          您需要在 Toast 上调用 .show() 才能显示它。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-01-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多