Code example for EditTextPreference

Methods: setOnPreferenceChangeListener, getEditText

0
	} 
	 
	public void initListeners() { 
		EditTextPreference serverMacPreference = (EditTextPreference) findPreference(EDITTEXT_SERVER_MAC);
		 
		serverMacPreference.getEditText().addTextChangedListener(
				new TextWatcher() {
					private static final String REGEX_MAC_ADDRESS_PARTIAL = "^([0-9A-F]{2}[:]){0,5}([0-9A-F]{0,2})$";
		 
					@Override 
					public void onTextChanged(CharSequence s, int start, int before, int count) {
						// do nothing 
					} 
		 
					@Override 
					public void beforeTextChanged(CharSequence s, int start, int count,
							int after) {
						// do nothing 
					} 
		 
					@Override 
					public void afterTextChanged(Editable s) {
						if (s.length() != 0 && (s.length() > 17 || !s.toString().matches(REGEX_MAC_ADDRESS_PARTIAL))) {					
							String mac = s.toString();
							Log.i(TAG,"Changing: " + mac);
							// strip colons 
							mac = mac.replaceAll(":", "");
							// replace bad chars 
							mac = mac.replaceAll("[^A-Fa-f0-9]", "0");
		 
							StringBuilder stringBuilder = new StringBuilder();
							int counter = 0;
							for (int i = 0; i < mac.length(); i++) { 
								counter++;
				                char currentChar = mac.charAt(i);
				                if((counter) % 3 == 0 && currentChar != ':') {
									counter++;
				                	stringBuilder.append(":");
				                } 
				                stringBuilder.append(Character.toUpperCase(currentChar));
				            } 
							 
				            int end = stringBuilder.length();
				            s.clear();
				            s.append(stringBuilder.substring(0, end <= 17 ? end : 17));
						} 
					} 
				}); 
		 
		serverMacPreference.setOnPreferenceChangeListener(
				new OnPreferenceChangeListener() {
					private static final String REGEX_MAC_ADDRESS = "^([0-9A-F]{2}[:]){5}([0-9A-F]{2})$";
			 
					@Override 
					public boolean onPreferenceChange(Preference preference, Object newValue) {