이 게시물은 대학 수업 과정을 들으며 혼자 연습한 뒤에 공유하는 게시물입니다.
따라서 완성도가 떨어질 수 있으니, 욕하지 마세요 ㅠ..ㅠ
★ 구동할 최소 안드로이드 SDK 사양
android:minSdkVersion="11"
android:targetSdkVersion="16"
package com.example.reservation;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.DatePicker;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener, OnCheckedChangeListener {private Button buttonReservationStart;
private Button buttonReservationComplete;
private RadioGroup radioGroup;
private DatePicker datePicker;
private TimePicker timePicker;
private Chronometer chronometerReservationTime;
private TextView reservationDateTimeText;
private TextView textReservationStatus;
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);buttonReservationStart = (Button)findViewById(R.id.buttonReservationStart);buttonReservationStart.setOnClickListener(this);
buttonReservationComplete = (Button)findViewById(R.id.buttonReservationCompleted);buttonReservationComplete.setOnClickListener(this);
datePicker = (DatePicker)findViewById(R.id.datePicker);timePicker = (TimePicker)findViewById(R.id.timePicker);chronometerReservationTime = (Chronometer)findViewById(R.id.chronometerReservationTime);reservationDateTimeText = (TextView) findViewById(R.id.textReservatedDateTime);textReservationStatus = (TextView) findViewById(R.id.textReservationStatus);radioGroup = (RadioGroup)findViewById(R.id.radioGroup);radioGroup.setOnCheckedChangeListener(this);
initViewObjects(false);
}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);return true;}/**
* @see android.view.View.OnClickListener#onClick(android.view.View)* @author : minam.cho* @methodName : onClick* @param v*/public void onClick(View v) {if(v instanceof Button) { // 클릭한 주체가 버튼이라면Button clickButtonObj = (Button) v;//현재 어떤 버튼이 클릭되었는지 확인
if(R.id.buttonReservationStart == clickButtonObj.getId()) { //[1] 예약 시작 버튼일 경우textReservationStatus.setText(getResources().getString(R.string.reservationStatusReservating));initViewObjects(true);
Toast.makeText(MainActivity.this, "Start reservation from now!!", Toast.LENGTH_SHORT).show();chronometerReservationTime.setBase(SystemClock.elapsedRealtime());chronometerReservationTime.start();reservationDateTimeText.setVisibility(View.INVISIBLE);reservationDateTimeText.setText(getResources().getString(R.string.none));if(radioGroup.getCheckedRadioButtonId() == R.id.radioChooseCalendar) {
datePicker.setVisibility(View.VISIBLE);timePicker.setVisibility(View.INVISIBLE);} else if(radioGroup.getCheckedRadioButtonId() == R.id.radioChooseTime){datePicker.setVisibility(View.INVISIBLE);timePicker.setVisibility(View.VISIBLE);}} else if(R.id.buttonReservationCompleted == clickButtonObj.getId()) { //[2] 예약 완료 버튼일 경우textReservationStatus.setText(R.string.setReservationCompleted);String reservationCompleted = String.format(getResources().getString(R.string.reservationCompleted));initViewObjects(false);
Toast.makeText(MainActivity.this, "Reservation completed!!", Toast.LENGTH_SHORT).show();int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth() + 1;
int year = datePicker.getYear();
int hour = timePicker.getCurrentHour();
int minute = timePicker.getCurrentMinute();
reservationDateTimeText.setVisibility(View.VISIBLE);reservationDateTimeText.setText("예약날짜: "+year+"년 "+month+"월 "+day+"일("+hour+":"+minute+")"+reservationCompleted);chronometerReservationTime.stop();}}}@Overridepublic void onCheckedChanged(RadioGroup rg, int arg1) {RadioButton radButton = (RadioButton) findViewById(rg.getCheckedRadioButtonId());if(radButton.getId() == R.id.radioChooseCalendar) {
datePicker.setVisibility(View.VISIBLE);timePicker.setVisibility(View.INVISIBLE);} else if(radButton.getId() == R.id.radioChooseTime){datePicker.setVisibility(View.INVISIBLE);timePicker.setVisibility(View.VISIBLE);}}private void initViewObjects(boolean makeDisable) {ViewGroup itemGroup = (ViewGroup)findViewById(R.id.itemGroup);for(int i=0; i < itemGroup.getChildCount(); i++) {View v = (View)itemGroup.getChildAt(i);v.setEnabled(makeDisable);}}}
- activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_alignParentLeft="true"android:layout_alignParentRight="true"android:layout_alignParentTop="true"android:gravity="fill_vertical|center_horizontal"android:orientation="vertical" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/txtCurrentTime"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/currentTime" /><DigitalClockandroid:id="@+id/digitalClock1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/textReservationStatus"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/reservationStatusWaiting"android:textAppearance="?android:attr/textAppearanceMedium" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/reservatedDuringTime"android:textAppearance="?android:attr/textAppearanceLarge" /><Chronometerandroid:id="@+id/chronometerReservationTime"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/chronoText" /><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/duringTime"android:textAppearance="?android:attr/textAppearanceSmall" /></LinearLayout><Buttonandroid:id="@+id/buttonReservationStart"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="0dp"android:gravity="center_vertical|center_horizontal"android:text="@string/reservationStart" /><LinearLayoutandroid:id="@+id/itemGroup"android:layout_width="match_parent"android:layout_height="0dp"android:orientation="vertical"android:layout_weight="1"><RadioGroupandroid:id="@+id/radioGroup"android:layout_width="match_parent"android:layout_height="wrap_content" ><RadioButtonandroid:id="@+id/radioChooseCalendar"android:layout_width="wrap_content"android:layout_height="wrap_content"android:checked="true"android:text="@string/setDate" /><RadioButtonandroid:id="@+id/radioChooseTime"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="@string/setTime" /></RadioGroup><DatePickerandroid:id="@+id/datePicker"android:layout_width="match_parent"android:layout_height="match_parent" /><TimePickerandroid:id="@+id/timePicker"android:layout_width="match_parent"android:layout_height="match_parent"android:visibility="invisible" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="#FDF5E6"android:textAlignment="center" ><Buttonandroid:id="@+id/buttonReservationCompleted"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/setReservationCompleted" /><TextViewandroid:id="@+id/textReservatedDateTime"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/none"android:visibility="invisible" /></LinearLayout></LinearLayout></RelativeLayout>
- strings.xml
<?xml version="1.0" encoding="utf-8"?><resources><string name="app_name">Reservation</string><string name="action_settings">Settings</string><string name="hello_world">Hello world!</string><string name="title">예약을 해 봅시다</string><string name="currentTime">현재 시간: </string><string name="reservatedDuringTime">예약에 걸린 시간: </string><string name="reservationStatusTitle">예약 진행 상태: </string><string name="reservationStatusWaiting">대기 중</string><string name="reservationStatusReservating">예약 중</string><string name="reservationStart">예약 시작</string><string name="reservationCompleted">예약됨</string><string name="chronoText">%s</string><string name="duringTime">초</string><string name="setDate">날짜 설정(DatePicker)</string><string name="setTime">시간 설정(TimePicker)</string><string name="setReservationCompleted">예약 완료</string><string name="none">-</string></resources>
'PlatForm > Android' 카테고리의 다른 글
안드로이드 시작하기 Chapter 1 - 4 (설치+이클립스 연동) (0) | 2012.08.15 |
---|---|
안드로이드 시작하기 - 이클립스 연동 + 안드로이드 SDK 설치 (0) | 2012.08.15 |
안드로이드 시작하기 - Hello World !!! (0) | 2012.08.15 |