首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >验证只允许在android中编辑文本中的数字和字符

验证只允许在android中编辑文本中的数字和字符
EN

Stack Overflow用户
提问于 2013-01-07 16:22:36
回答 7查看 75.8K关注 0票数 32

在我的应用程序中,我必须验证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中输入输入时,应用程序被强制关闭。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14192199

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档