<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout_form"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="120dp"
>
<TextView
android:id="@+id/viewText_name"
android:text="이 름"
android:textSize="20dp"
android:layout_width="100dp"
android:layout_height="34dp"
android:layout_marginRight="10dp"
android:gravity="center_horizontal"
/>
<EditText
android:id="@+id/text_name"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/viewText_name"
android:layout_alignBaseline="@+id/viewText_name"
android:maxLength="20"
android:maxLines="1"
android:inputType="text|textCapWords"
/>
<TextView
android:id="@+id/viewText_age"
android:text="나 이"
android:textSize="20dp"
android:layout_width="100dp"
android:layout_height="34dp"
android:layout_marginRight="10dp"
android:layout_below="@id/viewText_name"
android:gravity="center_horizontal"
/>
<EditText
android:id="@+id/text_age"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/viewText_age"
android:layout_alignBaseline="@+id/viewText_age"
android:maxLength="3"
android:maxLines="1"
android:inputType="number"
/>
<TextView
android:id="@+id/viewText_birthday"
android:text="생년월일"
android:layout_width="100dp"
android:layout_height="34dp"
android:layout_marginRight="10dp"
android:layout_below="@id/viewText_age"
android:textSize="20dp"
android:gravity="center_horizontal"
/>
</RelativeLayout>
<Button
android:id="@+id/button_birthday"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:onClick="onButtonBirthdayClicked"
/>
<Button
android:id="@+id/button_save"
android:text="저 장"
android:onClick="onButtonLoginClicked"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="20dp"
/>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
TextView viewText_name;
EditText text_name;
TextView viewText_age;
EditText text_age;
TextView viewText_birthday;
Button button_birthday;
Button button_save;
SimpleDateFormat sdf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewText_name = (TextView) findViewById(R.id.viewText_name);
text_name = (EditText) findViewById(R.id.text_name);
viewText_age = (TextView) findViewById(R.id.viewText_age);
text_age = (EditText) findViewById(R.id.text_age);
viewText_birthday = (TextView) findViewById(R.id.viewText_birthday);
button_birthday = (Button) findViewById(R.id.button_birthday);
button_save = (Button) findViewById(R.id.button_save);
sdf = new SimpleDateFormat("yyyy년 MM월 dd일");
button_birthday.setText( sdf.format(Calendar.getInstance().getTime()) );
}
public void onButtonBirthdayClicked(View v) {
Calendar c = Calendar.getInstance();
String today = button_birthday.getText().toString();
if(today.length() == 13) {
try {
c.setTime(sdf.parse(today));
} catch (ParseException e) {
}
}
DatePickerDialog datePickerDialog = new DatePickerDialog(
MainActivity.this,
android.R.style.Theme_Holo_Light_Dialog_MinWidth,
new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
try {
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, dayOfMonth);
button_birthday.setText( sdf.format(calendar.getTime()) );
} catch (Exception e) {
e.printStackTrace();
}
}
},
c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
datePickerDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
datePickerDialog.show();
}
public void onButtonLoginClicked(View v) {
Toast toast = Toast.makeText(getBaseContext(),
"이름 : " + text_name.getText() + ", " +
"나이 : " + text_age.getText() + ", " +
"생년월일 : " + button_birthday.getText(),
Toast.LENGTH_LONG);
toast.show();
}
}