【问题标题】:EditText Android Filter for IP Address ###.###.###.###?用于 IP 地址的 EditText Android 过滤器###.###.###.###?
【发布时间】:2015-10-10 08:27:08
【问题描述】:

我使用这个过滤器来验证 IP 地址

localIP.setHintTextColor(Color.parseColor("#aaaaaa"));
        InputFilter[] filters = new InputFilter[1];
        filters[0] = new InputFilter() {
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (end > start) {
                    String destTxt = dest.toString();
                    String resultingTxt = destTxt.substring(0, dstart) + source.subSequence(start, end) + destTxt.substring(dend);
                    if (!resultingTxt.matches ("^\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3})?)?)?)?)?)?")) {
                        return "";
                    } else {
                        String[] splits = resultingTxt.split("\\.");
                        for (int i=0; i<splits.length; i++) {
                            if (Integer.valueOf(splits[i]) > 255) {
                                return "";
                            }
                        }
                    }
                }
                return null;
            }
        };
        localIP.setFilters(filters);

允许的结果是##.##.##.##,我希望最大###.###.###.## #。我该怎么做?

【问题讨论】:

  • 为什么是\d{1,3}?如果您只想允许三个集群,\d{3} 可以解决问题。
  • 谢谢先生,我在我的代码中编辑了 ("^\\d{3}(\\.(\\d{3}(\\.(\\d{3}(\\ .(\\d{3})?)?)?)?)?)?")) ,但运行后无法输入edittext 先生
  • 哦。您的过滤器应该在EditText 输入被确认后设置。
  • 过滤器限制了您可以在编辑文本中输入的内容,并且由于您必须一次输入三个数字(这是不可能的,您一次只能输入一个数字),您可以不要输入任何东西。您需要在 edittext 输入完成后设置输入检查。
  • 您可以尝试onTextChangedListener 或添加一个按钮来检查其onClickListener 中的输入。

标签: android ip-address


【解决方案1】:
InputFilter[] filters = new InputFilter[1];
        filters[0] = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end,
                    android.text.Spanned dest, int dstart, int dend) {
                if (end > start) {
                    String destTxt = dest.toString();
                    String resultingTxt = destTxt.substring(0, dstart)
                            + source.subSequence(start, end)
                            + destTxt.substring(dend);
                    if (!resultingTxt
                            .matches("^\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3})?)?)?)?)?)?")) {
                        return "";
                    } else {
                        String[] splits = resultingTxt.split("\\.");
                        for (int i = 0; i < splits.length; i++) {
                            if (Integer.valueOf(splits[i]) > 255) {
                                return "";
                            }
                        }
                    }
                }
                return null;
            }

        };
        editTxxt.setFilters(filters);

或者看看

http://kmansoft.com/2011/02/27/an-edittext-for-entering-ip-addresses/

【讨论】:

    【解决方案2】:

    试试这个……

       localIP.addTextChangedListener(new TextWatcher() {
                        int cursorPosition=0;
                        @Override
                        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
                        }
    
                        @Override
                        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
                        }
    
                        @Override
                        public void afterTextChanged(Editable editable) {
                            localIP.removeTextChangedListener(this);
    
                            try{
                                cursorPosition= filterText.getSelectionStart();
                                if(editable.length() > 0){
    
                                    String tempIp = "";
                                    String newIp = "";
                                    String strIp = filterText.getText().toString();
                                    String tempParamArr[] = strIp.split("\\.");
                                    if(tempParamArr.length > 0){
                                        cursorPosition -= (tempParamArr.length-1);
                                        for(int i = 0; i < tempParamArr.length; i++) {
                                            tempIp += tempParamArr[i];
                                        }
                                    }else{
                                        tempIp = strIp;
                                    }
    
                                    for(int count = 0; count < tempIp.length(); count++) {
                                        if(count==3 || count==6||count==9||count==12){
                                            newIp += ".";
                                            newIp += tempIp.charAt(count);
                                            cursorPosition++;
                                        }else{
                                            newIp += tempIp.charAt(count);
                                        }
                                    }
                                    filterText.setText(newIp);
                                    if(newIp.length()>cursorPosition)
                                        filterText.setSelection(cursorPosition);
                                    else
                                        filterText.setSelection(newIp.length());
                                }
                            }catch(Exception e){
                                e.printStackTrace();
                            }
    
                            localIP.addTextChangedListener(this);
    
                        }
                    });
    

    【讨论】:

      猜你喜欢
      • 2016-12-04
      • 2022-12-02
      • 1970-01-01
      • 1970-01-01
      • 2013-09-07
      • 1970-01-01
      • 2019-03-25
      • 2023-03-27
      • 1970-01-01
      相关资源
      最近更新 更多