Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.text.InputType;

import android.widget.EditText;

import android.widget.TextView;

public class Main {
    public static void autoCompleteTime(CharSequence text, EditText time, TextView timeHint) {
        String stringText = text.toString();

        String textToBeSet = "";
        int inputType = InputType.TYPE_CLASS_DATETIME | InputType.TYPE_DATETIME_VARIATION_TIME;
        time.setError(null); //remove any error set from previously
        boolean error = false;

        if (stringText.matches("^\\d$")) {
            if (stringText.equals("1") || stringText.equals("0"))
                textToBeSet = "0:00AM";
            else {
                textToBeSet = ":00AM";
            }
        } else if (stringText.matches("^\\d\\d$")) {
            int intText = Integer.parseInt(stringText);
            if (intText >= 0 && intText <= 12)
                textToBeSet = ":00AM";
            else
                error = true;
        } else if (stringText.matches("^\\d+:$"))
            textToBeSet = "00AM";
        else if (stringText.matches("^\\d+:\\d$"))
            textToBeSet = "0AM";
        else if (stringText.matches("^\\d+:\\d\\d$")) {
            int intText = Integer.parseInt(stringText.replaceAll("^\\d+:", "")); //get minutes
            if (intText > 0 && intText < 60) {
                inputType = InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS;
                textToBeSet = "AM";
            } else
                error = true;
        } else if (stringText.matches("^\\d+:\\d\\d(A|P)$")) {
            inputType = InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS;
            textToBeSet = "M";
        } else if (stringText.equals(""))
            textToBeSet = "";
        else if (stringText.matches("^\\d+:\\d+(A|P)M")) {
            //To-do - take control to next input field
        } else {//error condition
            error = true;
        }

        if (error) {
            textToBeSet = "";
            time.setError("Incorrect time format");
        }

        time.setInputType(inputType);
        timeHint.setText(textToBeSet);

    }
}