ssummeraw
2013-03-25 05:19
采纳率: 73%
浏览 12.4k

Edittext 验证只允许输入数字和字符?

在程序中 edittext 验证,应该只允许输入字符、数字、下划线和连字符。

edittext.addTextChangedListener(new TextWatcher() {

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {
                            // TODO Auto-generated method stub

                            }

                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                                            // TODO Auto-generated method stub

                            }

                                        @Override
                            public void afterTextChanged(Editable s) {
                                            // validation codes here

                            location_name=s.toString();
                            Toast.makeText(getApplicationContext(),location_name, Toast.LENGTH_SHORT).show();                

                            if (location_name.matches(".*[^a-z^0-9].*")) 
                                {
                            location_name = location_name.replaceAll("[^a-z^0-9]", "");
                            s.append(location_name);
                            s.clear();
                            Toast.makeText(getApplicationContext(),"Only lowercase letters and numbers are allowed!",Toast.LENGTH_SHORT).show();

                            }

                            }

                        });     

location.add(location_name);

当我在edittext中输入信息时,程序被强行关闭了。什么地方出错呢?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • Curie-87 2013-03-26 07:54
    已采纳

    不要使用 "manual"方法,下面这个方法很简单:

    InputFilter filter = new InputFilter() { 
        public CharSequence filter(CharSequence source, int start, int end, 
    Spanned dest, int dstart, int dend) { 
                for (int i = start; i < end; i++) { 
                        if ( !Character.isLetterOrDigit(source.charAt(i)) || !Character.toString(source.charAt(i)) .equals("_") || !Character.toString(source.charAt(i)) .equals("-")) { 
                                return ""; 
                        } 
                } 
                return null; 
        } 
    }; 
    edit.setFilters(new InputFilter[]{filter});
    

    或者另一个方法:在 xml 中设置创建 EditText 允许的字符

    <EditText 
      android:inputType="text" 
      android:digits="0,1,2,3,4,5,6,7,8,9,*,qwertzuiopasdfghjklyxcvbnm,_,-" 
      android:hint="Only letters, digits, _ and - allowed"
    />
    
    已采纳该答案
    打赏 评论
  • franzhong 2013-03-25 12:31

    你放错地也改错值了,给你个参考的

         final EditText   et = (EditText) this.findViewById(R.id.myEdit);
         et.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                 String location_name=s.toString();       
                 if (location_name.matches("^[A-Za-z0-9 _-]+$")){
                    Toast.makeText(getApplicationContext(),"类型正确!",Toast.LENGTH_SHORT).show();
                 }else{
                    Toast.makeText(getApplicationContext(),"类型不符!",Toast.LENGTH_SHORT).show();                  
                    et.setText("AAA");//change here
                 }
                }......
    
    打赏 评论
  • 竹林听夜风 2016-12-02 06:07

    android:digits="0,1,2,3,4,5,6,7,8,9,*,qwertzuiopasdfghjklyxcvbnm,_,-" 中间不需要逗号的 写成这样就可以了的 android:digits="0123456789*qwertzuiopasdfghjklyxcvbnm_-"

    打赏 评论

相关推荐 更多相似问题