在PreferenceFragment中验证edittext

时间:2014-10-27 16:37:17

标签: android xml sharedpreferences preferenceactivity

我有一个EditTextPreference,供用户在我的preferences.xml文件中输入蓝牙MAC地址。我使用android:digits来限制有效字符的输入,但这不会阻止某人添加太少或太多的字符,双冒号等。如果他们做这些事情,应用程序崩溃时无效的MAC地址异常。有没有办法以编程方式执行此操作?

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">


    <PreferenceCategory android:title="Bluetooth Settings">

        <EditTextPreference
            android:key="prefMacAddress"
            android:title="MAC Address"
            android:lines="1"
            android:maxLines="1"
            android:summary="Enter MAC Address of the Module"
            android:dialogTitle="MAC Address"
            android:dialogMessage="Enter MAC Address of the Module"   
            android:defaultValue="20:13:07:26:24:32"
            android:digits="0123456789abcdef:"/>

    </PreferenceCategory>

</PreferenceScreen>

1 个答案:

答案 0 :(得分:0)

在EditText中使用正则表达式输入。这是一个很好的教程:http://www.vogella.com/tutorials/JavaRegularExpressions/article.html

相关问题