Deactivate keyboard numbers in android studio so that only allows entering 0 and 1

0

This time I am in a dilemma, I am creating an application that transforms a sentence or word given to its equivalent in exadecimal, this with the purpose of generating a bsc frame and printing it so that the user can visualize it, but when moving on the project I came across the problem that in android studio I can use the keyboard in numerical type, but I still accept numbers other than 1 and 0, I need that you can only enter 1 and 0 this in order to enter the header , error detection and the other parts that make up the bsc frame all must be in binary, and I do not know how to deactivate the numbers on the keyboard other than 1 and 0, if someone could give me a solution, use some condition a try catch or some way to deactivate the numbers would be appreciated.

I leave the link to GitHub of my project in android studio for those who like to help me.

link

thanks.

    
asked by a. valenzuela 15.09.2018 в 06:04
source

1 answer

0

To do what you want, the simplest thing is to make yourself a validating method, to which you would pass the view id parameter, if the validator does not pass a message with the type of error.

private boolean isValid(Int id) {
        EditText valorView = (EditText)findViewById(id);
        String valorString = valorView.getText().toString().trim();
        if(valorString.isEmpty()){
           showSnackBar(valorView,"Por favor ingrese un valor")
        }
        switch (id){
          case R.id.IngValor:
               //pon tu validacion solo para este TextView aqui
               if(condicionValida){
                 showSnackBar(valorView,"Valor valido");
               } else {
                 return false;
               }
               break;
          case R.id.MostValor:
               //pon tu validacion solo para este TextView aqui
               //Para comprobar que solo se han introducido 0 y 1:
               for(int j= 0;j<valorString.length();j++){
                    if(Character.isDigit(cade.charAt(j))){
                        if(!Character.valueOf(valorString.charAt(j)).equals('0') || !Character.valueOf(valorString.charAt(j)).equals('1')){
                            showSnackBar(valorView,"Por favor introduzca solo ceros y unos");
                            return false;
                        }
                    } else {
                        showSnackBar(valorView,"Por favor introduzca solo ceros y unos");
                        return false;
                    }
                }
               break;
          case R.id.MostValor2:
               //pon tu validacion solo para este TextView aqui
               break;
          case R.id.MostValor3:
               //pon tu validacion solo para este TextView aqui
               break;
          //Puedes validar mas vistas......
        }
        return true;
    }

private void showSnackBar(View v, String message) {
    Snackbar.make(v, message, Snackbar.LENGTH_LONG).show();
} 
    
answered by 15.09.2018 / 12:01
source