2

我有一个 EditText,我希望用户能够输入 MAC 地址。

如何限制用户仅输入 A、B、C..F 和数字并以正确的格式 (00:11:22:33:44:55) 写入?

如何验证?

编辑:我发现这段代码可以满足我的需要,但有一个 IP 地址。我尝试使用您建议的正则表达式,但它们不起作用。

    public void showIP(){
    // Set an EditText view to get user input 
    final EditText input = new EditText(this);
    input.setInputType(InputType.TYPE_CLASS_PHONE);
    input.setSingleLine();
    InputFilter[] filters = new InputFilter[1];
    //filter used to allow only IPv4 style address
    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("^[\\dA-F]{2}(?::[\\dA-F]{2}){5}$")) { 
                    return "";
                } /*else {
                    String[] splits = resultingTxt.split("\\:");
                    for (int i=0; i<splits.length; i++) {
                        if (Integer.valueOf(splits[i]) > ) {
                            return "";
                        }
                    }
                }*/
            }
        return null;
        }
    };
    input.setFilters(filters);

    input.setText(serverIP);
    new AlertDialog.Builder(this)
        .setTitle(R.string.ServerIP)
        .setMessage("Change SERVER IP")
        .setView(input)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                serverIP=input.getText().toString();
                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor = settings.edit();
                editor.putString("IP", input.getText().toString());
                // Commit the edits!
                editor.commit();
                view.updateServerIP();
            }
        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                //do nothing
            }
        }).show();
}
4

3 回答 3

6

在此代码中,MAC 地址字段将自动生成 ':' 。您也可以删除该条件。

final EditText input = new EditText(context);
        input.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
        input.setSingleLine();
        InputFilter[] filters = new InputFilter[1];

        filters[0] = new InputFilter() {


            @Override
            public CharSequence filter(CharSequence source, int start, int end,
                    Spanned dest, int dstart, int dend) {
                // TODO Auto-generated method stub
                if (end > start) {
                    String destTxt = dest.toString();
                    String resultingTxt = destTxt.substring(0, dstart) + source.subSequence(start, end) + destTxt.substring(dend);
                    if (resultingTxt.matches("([0-9a-fA-F][0-9a-fA-F]:){0,5}[0-9a-fA-F]"))
                    {

                    }
                    else if(resultingTxt.matches("([0-9a-fA-F][0-9a-fA-F]:){0,4}[0-9a-fA-F][0-9a-fA-F]")){
                        return source.subSequence(start, end)+":" ;
                    }
                    else if(resultingTxt.matches("([0-9a-fA-F][0-9a-fA-F]:){0,5}[0-9a-fA-F][0-9a-fA-F]")){

                    }
                    else
                    { 
                        return "";
                    } 
                }
                return null;
            }
        };
        input.setFilters(filters);
于 2014-05-11T11:57:36.680 回答
0

The general regex to match a char from 0-F would be [\dA-F]

So having that in mind, you could do: ^[\dA-F]{2}(?:[\-][\dA-F]{2}){5}$

Nothe: The (?: Is for a non capturing group.

DEMO

于 2012-11-26T17:15:47.787 回答
0

([\da-fA-F]{2}(?:\:|$)){6}

这很好用,你可以在这里看到:

http://regexr.com?32ugi

如果您希望将“-”也包含在分隔符中(根据标准有效),您可以使用以下命令:

([\da-fA-F]{2}(?:\:|-|$)){6}

http://regexr.com?32ugl

于 2012-11-26T18:55:37.937 回答