public DescriptorFormatWatcher(@Nullable SlotsParser slotsParser, @Nullable MaskDescriptor maskDescriptor) { this.slotsParser = slotsParser; this.maskDescriptor = maskDescriptor; if (maskDescriptor != null) { changeMask(maskDescriptor); } }
public void changeMask(@NonNull final MaskDescriptor maskDescriptor) { this.maskDescriptor = maskDescriptor; refreshMask(maskDescriptor.getInitialValue()); }
@SuppressLint("SetTextI18n") @Test public void cursorPosition() { DescriptorFormatWatcher watcher = new DescriptorFormatWatcher(MaskDescriptor.ofSlots(PredefinedSlots.RUS_PHONE_NUMBER)); assertEquals(0, watcher.getCursorPosition()); TextView textView = new TextView(RuntimeEnvironment.application); watcher.installOn(textView); textView.setText("9"); // +7_(9 assertEquals(5, watcher.getCursorPosition()); textView.setText(null); assertEquals(0, watcher.getCursorPosition()); textView.setText("999"); // +7_(999)_ assertEquals(9, watcher.getCursorPosition()); textView.setText("999876"); // +7_(999)_876- assertEquals(13, watcher.getCursorPosition()); textView.setText("9998765432111111111"); // +7_(999)_876-54-32 assertEquals(18, watcher.getCursorPosition()); textView.setText("7"); // +7_( assertEquals(4, watcher.getCursorPosition()); }
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_static_mask); dataEdit = (EditText) findViewById(R.id.editData); formatWatcher = new DescriptorFormatWatcher(MaskDescriptor.emptyMask().setTerminated(false)); formatWatcher.installOn(dataEdit); maskPreviewView = (TextView) findViewById(R.id.textMaskPreview); maskPreviewView.setText(getString(R.string.mask_preview, "")); findViewById(R.id.buttonMask).setOnClickListener(this); final Mask mask = MaskImpl.createTerminated(PredefinedSlots.RUS_PHONE_NUMBER); mask.setPlaceholder('*'); mask.setShowingEmptySlots(true); Log.d("Mask", mask.toString()); mask.insertFront("999"); Log.d("Mask", mask.toString()); }
@Test public void getUnformattedString() { DescriptorFormatWatcher watcher = new DescriptorFormatWatcher(MaskDescriptor.ofSlots(PredefinedSlots.RUS_PHONE_NUMBER)); assertEquals("+7", watcher.getMask().toUnformattedString()); }
@Override public void onMaskSelected(@NonNull MaskDescriptor maskDescriptor, @NonNull String title) { formatWatcher.changeMask(maskDescriptor.setInitialValue(dataEdit.getText().toString())); maskPreviewView.setText(getString(R.string.mask_preview, maskDescriptor.toString())); } }