在Android上以正确的格式输入MAC地址

时间:2012-11-26 17:11:31

标签: android regex validation

我有一个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();
}

3 个答案:

答案 0 :(得分:5)

在此代码中,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);

答案 1 :(得分:0)

匹配0-F中的字符的一般正则表达式为[\dA-F]

考虑到这一点,你可以做到:^[\dA-F]{2}(?:[\-][\dA-F]{2}){5}$

Nothe:(?:适用于非捕获组。

DEMO

答案 2 :(得分:0)

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

这很有效,你可以在这里看到:

http://regexr.com?32ugi

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

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

http://regexr.com?32ugl