main.xml
********************************************************************************
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical">
<EditText android:id="@+id/airedDate" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:focusableInTouchMode="false" />
</LinearLayout>
********************************************************************************
MainActivity.java
********************************************************************************
public class MainActivity extends Activity {
static final int AIRED_DATE_DIALOG_ID = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText airedDate = (EditText) findViewById(R.id.airedDate);
airedDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(AIRED_DATE_DIALOG_ID);
}
});
Calendar c1 = Calendar.getInstance();
String yyyy = Integer.toString(c1.get(Calendar.YEAR));
String mm = Integer.toString(c1.get(Calendar.MONTH)+1);
String dd = Integer.toString(c1.get(Calendar.DAY_OF_MONTH));
if (mm.length() < 2)
mm = "0"+mm;
if (dd.length() < 2)
dd = "0"+dd;
airedDate.setText(yyyy+"-"+mm+"-"+dd);
}
private DatePickerDialog.OnDateSetListener airedDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String s = Utils.toDate(year, monthOfYear+1, dayOfMonth, "-");
EditText airedDate = (EditText) findViewById(R.id.airedDate);
airedDate.setText(s);
}
};
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case AIRED_DATE_DIALOG_ID:
EditText airedDate = (EditText) findViewById(R.id.airedDate);
Calendar c = Calendar.getInstance();
try {
c = Utils.getCalendar(airedDate.getText().toString());
} catch (Exception e) {
}
return new DatePickerDialog(this, airedDateSetListener, c.get(Calendar.YEAR), c.get(Calendar.MONDAY), c.get(Calendar.DAY_OF_MONTH));
}
return null;
}
}
********************************************************************************
Utils.java
********************************************************************************
public class Utils {
public static Calendar getCalendar(String yyyy, String mm, String dd) throws Exception {
Calendar c = Calendar.getInstance();
try {
int y = Integer.parseInt(yyyy);
int m = Integer.parseInt(mm)-1;
int d = Integer.parseInt(dd);
c.set(Calendar.YEAR, y);
c.set(Calendar.MONTH, m);
c.set(Calendar.DAY_OF_MONTH, d);
} catch (Exception e) {
throw e;
}
return c;
}
public static Calendar getCalendar(String yyyymmdd) throws Exception {
String[] ymd = null;
try {
ymd = yyyymmdd.split("-");
} catch (Exception e) {
throw e;
}
return getCalendar(ymd[0], ymd[1], ymd[2]);
}
public static String toDate(int yyyy, int mm, int dd, String sep) {
String result = Integer.toString(yyyy);
result += "-";
if (mm < 10)
result += "0"+Integer.toString(mm);
else
result += Integer.toString(mm);
result += "-";
if (dd < 10)
result += "0"+Integer.toString(dd);
else
result += Integer.toString(dd);
return result;
}
}
********************************************************************************
댓글 없음:
댓글 쓰기