2013-03-05 58 views
-1

在我的EditText中,我想输入第一个字符作为alpha,剩下的就是什么。我在TextWatcher的帮助下完成了这项任务。但现在我的问题是,如果我输入了错误的东西(比如数字,特殊字符)作为我的第一个字符,那么我的EditText不应该接受剩余的字符。如果我纠正了我的第一个字符,那么只有我的EditText应该接受。他们有可能实现这个朋友吗?如果是的话请引导我的朋友。使EditText只接受alpha作为第一个字符

我textWatcher代码是提前

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(); 
      } 
     } 
    } 
}); 

感谢。

+0

不,但\ /那个人确实;-) – 2013-03-05 07:26:28

+0

如果你不包括你在问题中尝试过的内容,你通常会得到赞成票。如果没有包含它,任何人都无法告诉你,在请求某人为你解决问题之前,你确实已经努力工作。因此,养成在未来的所有问题中包括你所尝试过的东西(代码特别有用)的习惯。 – yarian 2013-03-05 07:32:25

+1

从查看Amit的答案,我还会建议您在发布问题之前在SO中进行更多搜索。看起来你的问题已经被问到过了。不需要复制那里的信息。记住这个网站的重点。 – yarian 2013-03-05 07:33:28

回答

3

试试下面的代码

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 
      } 


     } 
    }); 
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) { 
      } 
     }); 
0

您可以使用方法chatAt(INT指数)在指定的位置得到字符的值,你的情况为0。

那么你应该使用isLetter()来验证提取的字符是字母。

例如。 isLetter(chatAt(0))

相关问题