-1

In my EditText I want to enter the first character as alpha and the remaining is whatever. I achieved this task with the help of TextWatcher. But now my problem is if I entered something wrong(like digits, special characters) as my First character then my EditText shouldn't accept the remaining characters. If I correct my first character then only my EditText should accept. Is their any possibility to achieve this friends? If yes then please guide me friends.

My textWatcher code is

edittext.addTextChangedListener(new TextWatcher() {
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {

    }

    public void afterTextChanged(Editable s) {          
        if (s.length() > 0) {
            String str = edittext.getText().toString();
            char t = str.charAt(0);
            if (!Character.isLetter(t)) {
                Toast.makeText(getApplicationContext(),
                        "please enter your first charecter as alpha",
                        Toast.LENGTH_LONG).show();
            }
        }
    }
});

Thanks in advance.

Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
  • No, but \/ that person does ;-) – Alex Gittemeier Mar 05 '13 at 07:26
  • You will usually get downvotes if you don't include what you tried in the question to begin with. Without including it there's no way for anyone to tell that you actually worked hard on this before asking for someone to solve the problem for you. So get into the habit of including what you've tried (code is specially helpful) in all your future questions. – yarian Mar 05 '13 at 07:32
  • 1
    From looking at Amit's answer, I will also recommend that you do more searching in SO before posting a question. It seems like your question has been asked before. No need for duplicating the information that's out there. Remember the point of this site. – yarian Mar 05 '13 at 07:33
  • See these links, [A Good Answer](http://stackoverflow.com/questions/6715112/help-need-example-android-regular-expression-accept-only-number-in-edittext) [Another good answer](http://stackoverflow.com/questions/10528601/regex-help-for-inputfilter-for-edittext-in-android) [one more answer](http://stackoverflow.com/questions/2763022/android-how-can-i-validate-edittext-input) [and another similar answer](http://stackoverflow.com/questions/4180770/allow-only-selected-charcters-based-on-regex-in-an-edittext) [A Google Search](https://www.google.co.in/search?q=regex%20for%20Edittext%20in%20android&aq – Amit Mar 05 '13 at 07:28

3 Answers3

3

try below code

editText.addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence s, int start,
                int before, int count) {

        }

        public void beforeTextChanged(CharSequence s, int start,
                int count, int after) {
        }

        public void afterTextChanged(Editable s) {

            Character cr = s.toString().charAt(0);
     if(Character.isLetter(cr))
      {
        // do stuff here  
      }
             else
             {
                // do stuff here
             }


        }
    });
Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75
0
editText.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence s, int start,
                    int before, int count) {
                            //check if 's' starts with an alphabet
                            if(Character.isLetter(s.toString().charAt(0)))
                            {
                                  //success
                            } else {
                                  //fail
                            }
            }

            public void beforeTextChanged(CharSequence s, int start,
                    int count, int after) {
            }

            public void afterTextChanged(Editable s) {
            }
        });
Kartik
  • 7,677
  • 4
  • 28
  • 50
0

You can use method chatAt(int index) to get the value of character at the specified position, in your case it is 0.

then you should use isLetter() to verify that fetched character is letter.

eg. isLetter(chatAt(0))