Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

public class Main {
    /**
     * Formats the text while the user types for Expiration Date
     * @param et The edit text being worked on
     * @param textWatcher Text watcher to both remove and add back on. (Pass 'this' from class)
     * @param <T> T extends EditText
     */
    public static <T extends EditText> void formatExpirationDateWhileTyping(T et, TextWatcher textWatcher) {
        if (et == null) {
            return;
        }
        if (textWatcher == null) {
            return;
        }
        Editable s = et.getText();
        if (s == null) {
            return;
        }

        if (s.length() > 0) {
            String input = s.toString();
            input = input.replace("/", "");
            input = input.replace(" / ", "");
            if (input.length() <= 2) {
            } else if (input.length() >= 3 && input.length() <= 7) {
                String sub1 = input.substring(0, 2);
                String sub2 = input.substring(2);
                String toSet = sub1 + "/" + sub2;

                et.removeTextChangedListener(textWatcher);
                et.setText(toSet);
                et.setSelection(toSet.length());
                et.addTextChangedListener(textWatcher);
            }
        }
    }
}