0

EditText を 2 文字と 5 桁の数字で構成してください。正規表現を使おうと思ったのですが、使い方がわかりません。前もって感謝します

4

2 に答える 2

0
boolean validate(String str){
    int letters = 0;
    int numbers = 0;
    int others = 0;
    for(int i=0; i<str.length(); i++){
        char ch = str.getAt(i);
        if(Character.isLetter(ch)){
            letters++;
        }
        else if (Character.isDigit(ch)){
            numbers++;
        }
        else{
            others++;
        }
    }
    return others == 0 && numbers==5 && letters == 2;
}
于 2013-03-13T18:40:06.250 に答える
0
EditText edit = (EditText) this.findViewById(R.id.edit_text);


  edit.addTextChangedListener(new TextWatcher() {            
    @Override
    public void onTextChanged(CharSequence s, int start, 
                              int before, int count) {
    char[] c_arr = s.toCharArray();
    int letterCount=0;
    int digitCount=0;

        for(Character c : c_arr){

            try{
                Integer.parseInt(c.toString())
                digitCount++;
                if(digitCount > 2){
                    // alert here
                    s = s.substring(0, s.length() - 1);
                    EditText edit = (EditText) this.findViewById(R.id.edit_text);
                    edit.setText(s);
                    break;
                }
            }catch (NumberFormatException e) {
                letterCount++;
                if(letterCount > 5){
                    // alert here
                    s = s.substring(0, s.length() - 1);
                    EditText edit = (EditText) this.findViewById(R.id.edit_text);
                    edit.setText(s);
                    break;
                }
            }
        }    
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start,
                                  int count, int after) {}

    @Override
    public void afterTextChanged(Editable s) {

    }
  });
于 2013-03-13T18:46:35.370 に答える