247 lines
6.5 KiB
Java
247 lines
6.5 KiB
Java
package dbfields;
|
||
|
||
import android.content.Context;
|
||
import android.os.Parcel;
|
||
import android.os.Parcelable;
|
||
import android.text.InputFilter;
|
||
import android.text.InputType;
|
||
import android.util.AttributeSet;
|
||
import android.view.ViewGroup;
|
||
import android.widget.EditText;
|
||
import android.widget.LinearLayout;
|
||
import android.widget.TextView;
|
||
|
||
import java.text.ParseException;
|
||
import java.text.SimpleDateFormat;
|
||
import java.util.Date;
|
||
|
||
import kz.istt.locust.R;
|
||
|
||
public class DateInput extends LinearLayout
|
||
{
|
||
private EditText etD = null;
|
||
private TextView tv1 = null; //Разделитель дня и месяца
|
||
private EditText etM = null;
|
||
private EditText etY = null;
|
||
|
||
//Задаёт видимость полей
|
||
public void setVisibleDMY(String val)
|
||
{
|
||
if(val.toLowerCase().indexOf("d")==-1)
|
||
{
|
||
etD.setVisibility(INVISIBLE);
|
||
if(etD.getParent()!=null) ((ViewGroup) etD.getParent()).removeView(etD);
|
||
if(tv1.getParent()!=null) ((ViewGroup) tv1.getParent()).removeView(tv1);
|
||
} else
|
||
{
|
||
etD.setVisibility(VISIBLE);
|
||
if(etD.getParent()==null) addView(etD);
|
||
if(tv1.getParent()==null) addView(tv1);
|
||
}
|
||
|
||
if(val.toLowerCase().indexOf("m")==-1)
|
||
{
|
||
etM.setVisibility(INVISIBLE);
|
||
if(etM.getParent()!=null)
|
||
((ViewGroup) etM.getParent()).removeView(etM);
|
||
if(tv1.getParent()!=null)
|
||
((ViewGroup) tv1.getParent()).removeView(tv1);
|
||
} else
|
||
{
|
||
etM.setVisibility(VISIBLE);
|
||
if(etM.getParent()==null) addView(etM);
|
||
if(tv1.getParent()==null) addView(tv1);
|
||
}
|
||
}
|
||
|
||
public DateInput(Context context, AttributeSet attr)
|
||
{
|
||
super(context, attr);
|
||
|
||
int maxLength = 10;
|
||
InputFilter[] fArray = new InputFilter[1];
|
||
fArray[0] = new InputFilter.LengthFilter(maxLength);
|
||
|
||
LinearLayout.LayoutParams paramMessage = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
||
paramMessage.weight = (float) 1.0;
|
||
|
||
etD = new EditText(context);
|
||
etD.setHint(context.getResources().getString(R.string.Day));
|
||
etD.setInputType(InputType.TYPE_CLASS_NUMBER);
|
||
etD.setFilters(new InputFilter[] { new InputFilter.LengthFilter(2) }); // Почему-то не работает…
|
||
etD.setMaxEms(10);
|
||
etD.setFilters(fArray);
|
||
addView(etD, paramMessage);
|
||
|
||
tv1 = new TextView(context);
|
||
tv1.setText(".");
|
||
addView(tv1);
|
||
|
||
etM = new EditText(context);
|
||
etM.setHint(context.getResources().getString(R.string.Month));
|
||
etM.setInputType(InputType.TYPE_CLASS_NUMBER);
|
||
etM.setFilters(new InputFilter[] { new InputFilter.LengthFilter(2) }); // Почему-то не работает…
|
||
etM.setMaxEms(10);
|
||
etM.setFilters(fArray);
|
||
addView(etM, paramMessage);
|
||
|
||
TextView tv = new TextView(context);
|
||
tv.setText(".");
|
||
addView(tv);
|
||
|
||
etY = new EditText(context);
|
||
etY.setHint(context.getResources().getString(R.string.Year));
|
||
etY.setInputType(InputType.TYPE_CLASS_NUMBER);
|
||
etY.setFilters(new InputFilter[] { new InputFilter.LengthFilter(4) }); // Почему-то не работает…
|
||
etY.setMaxEms(10);
|
||
etY.setMinWidth(80);
|
||
etY.setFilters(fArray);
|
||
addView(etY, paramMessage);
|
||
}
|
||
|
||
/**
|
||
* Секунд с начала 1970 года
|
||
*/
|
||
public void setDate(Long date)
|
||
{
|
||
if (date == null)
|
||
setDate((Date) null);
|
||
else
|
||
setDate(new Date(date * 1000L));
|
||
}
|
||
|
||
public void setDate(Date date)
|
||
{
|
||
if (date == null)
|
||
{
|
||
etD.setText("");
|
||
etM.setText("");
|
||
etY.setText("");
|
||
} else
|
||
{
|
||
etD.setText(new SimpleDateFormat("dd").format(date));
|
||
etM.setText(new SimpleDateFormat("MM").format(date));
|
||
etY.setText(new SimpleDateFormat("yyyy").format(date));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Получить секунд с начала 1970 года по гринвичу
|
||
*
|
||
* @return
|
||
*/
|
||
public Long getDate()
|
||
{
|
||
Date d = null;
|
||
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
|
||
try
|
||
{
|
||
String day = etD.getText().toString();
|
||
if(day.equals("") && etD.getVisibility() == INVISIBLE) day="01"; //Если не виден и не задан день то 1е число
|
||
if(!day.equals(""))
|
||
{
|
||
if(Integer.valueOf(day)<0 || Integer.valueOf(day)>31) return null;
|
||
}
|
||
String month = etM.getText().toString();
|
||
if(month.equals("") && etM.getVisibility() == INVISIBLE) month="01"; //Если не виден и не задан день то 1е число
|
||
if(!month.equals(""))
|
||
{
|
||
if(Integer.valueOf(month)<0 || Integer.valueOf(month)>12) return null;
|
||
}
|
||
String year = etY.getText().toString();
|
||
if(year.equals("") && etY.getVisibility() == INVISIBLE) year="1970";
|
||
if(!year.equals(""))
|
||
{
|
||
if(Integer.valueOf(year)<1970 || Integer.valueOf(year)>2999) return null;
|
||
}
|
||
d = sdf.parse(day + "/" + month + "/" + year);
|
||
} catch (ParseException e)
|
||
{
|
||
return null;
|
||
}
|
||
return d.getTime() / 1000L;
|
||
}
|
||
|
||
@Override
|
||
public String toString()
|
||
{
|
||
return String.valueOf(getDate());
|
||
}
|
||
|
||
@Override
|
||
public boolean isSaveEnabled()
|
||
{
|
||
return true;
|
||
}
|
||
|
||
// Класс для сохранения изменений сделаных пользователем при повороте экрана
|
||
public static class SavedStateDI extends BaseSavedState
|
||
{
|
||
//Не может быть null почемуто вылетает!
|
||
String day = "";
|
||
String month = "";
|
||
String year = "";
|
||
|
||
SavedStateDI(Parcelable superState)
|
||
{
|
||
super(superState);
|
||
}
|
||
|
||
@Override
|
||
public void writeToParcel(Parcel out, int flags)
|
||
{
|
||
super.writeToParcel(out, flags);
|
||
out.writeString(day);
|
||
out.writeString(month);
|
||
out.writeString(year);
|
||
}
|
||
|
||
public static final Parcelable.Creator<SavedStateDI> CREATOR = new Parcelable.Creator<SavedStateDI>()
|
||
{
|
||
public SavedStateDI createFromParcel(Parcel in)
|
||
{
|
||
return new SavedStateDI(in);
|
||
}
|
||
|
||
public SavedStateDI[] newArray(int size)
|
||
{
|
||
return new SavedStateDI[size];
|
||
}
|
||
};
|
||
|
||
private SavedStateDI(Parcel in)
|
||
{
|
||
super(in);
|
||
day = in.readString();
|
||
month = in.readString();
|
||
year = in.readString();
|
||
}
|
||
}
|
||
|
||
@Override
|
||
protected Parcelable onSaveInstanceState()
|
||
{
|
||
SavedStateDI st = new SavedStateDI(super.onSaveInstanceState());
|
||
st.day = etD.getText().toString();
|
||
st.month = etM.getText().toString();
|
||
st.year = etY.getText().toString();
|
||
return st;
|
||
}
|
||
|
||
@Override
|
||
protected void onRestoreInstanceState(Parcelable state)
|
||
{
|
||
if (state == null || !(state instanceof SavedStateDI))
|
||
{
|
||
super.onRestoreInstanceState(state);
|
||
return;
|
||
}
|
||
SavedStateDI ss = (SavedStateDI) state;
|
||
super.onRestoreInstanceState(ss.getSuperState());
|
||
etD.setText(ss.day);
|
||
etM.setText(ss.month);
|
||
etY.setText(ss.year);
|
||
};
|
||
|
||
}
|