태터데스크 관리자

도움말
닫기
적용하기   첫페이지 만들기

태터데스크 메시지

저장하였습니다.

Android - 시간 및 날짜와 관련된 위젯을 이용한 "날짜/시간 예약을 흉내 낸 앱" 만들기 (샘플)

이 게시물은 대학 수업 과정을 들으며 혼자 연습한 뒤에 공유하는 게시물입니다.

따라서 완성도가 떨어질 수 있으니, 욕하지 마세요 ㅠ..ㅠ

 

★ 구동할 최소 안드로이드 SDK 사양

        android:minSdkVersion="11"
        android:targetSdkVersion="16"

 

  • 시간 및 날짜와 관련된 위젯을 이용한 “날짜/시간 예약을 흉내 낸 앱” 만들기 (샘플)

    • Design
      • 002
    • src
      • MainActivity.java
      • 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;
        
            
        
            @Override
        
            protected 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);
        
            }
        
            @Override
        
            public 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();
        
                    }
        
                }
        
            }
        
            @Override
        
            public 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);
        
                }
        
            }
        
        }
        
    • res/layout
      • 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" >
        
            <LinearLayout
        
                android: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" >
        
                <LinearLayout
        
                    android:layout_width="match_parent"
        
                    android:layout_height="wrap_content" >
        
                    <TextView
        
                        android:id="@+id/txtCurrentTime"
        
                        android:layout_width="wrap_content"
        
                        android:layout_height="wrap_content"
        
                        android:text="@string/currentTime" />
        
                    <DigitalClock
        
                        android:id="@+id/digitalClock1"
        
                        android:layout_width="wrap_content"
        
                        android:layout_height="wrap_content"
        
                        android:text="" />
        
                </LinearLayout>
        
                <LinearLayout
        
                    android:layout_width="match_parent"
        
                    android:layout_height="wrap_content" >
        
                    <TextView
        
                        android:id="@+id/textReservationStatus"
        
                        android:layout_width="wrap_content"
        
                        android:layout_height="wrap_content"
        
                        android:text="@string/reservationStatusWaiting"
        
                        android:textAppearance="?android:attr/textAppearanceMedium" />
        
                </LinearLayout>
        
                <LinearLayout
        
                    android:layout_width="match_parent"
        
                    android:layout_height="wrap_content"
        
                    android:gravity="center" >
        
                    <TextView
        
                        android:id="@+id/textView1"
        
                        android:layout_width="wrap_content"
        
                        android:layout_height="wrap_content"
        
                        android:text="@string/reservatedDuringTime"
        
                        android:textAppearance="?android:attr/textAppearanceLarge" />
        
                    <Chronometer
        
                        android:id="@+id/chronometerReservationTime"
        
                        android:layout_width="wrap_content"
        
                        android:layout_height="wrap_content"
        
                        android:text="@string/chronoText" />
        
                    <TextView
        
                        android:id="@+id/textView2"
        
                        android:layout_width="wrap_content"
        
                        android:layout_height="wrap_content"
        
                        android:text="@string/duringTime"
        
                        android:textAppearance="?android:attr/textAppearanceSmall" />
        
                </LinearLayout>
        
                <Button
        
                    android: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" />
        
                <LinearLayout
        
                    android:id="@+id/itemGroup"
        
                    android:layout_width="match_parent"
        
                    android:layout_height="0dp"
        
                    android:orientation="vertical" 
        
                    android:layout_weight="1">
        
                    <RadioGroup
        
                        android:id="@+id/radioGroup"
        
                        android:layout_width="match_parent"
        
                        android:layout_height="wrap_content" >
        
                        <RadioButton
        
                            android:id="@+id/radioChooseCalendar"
        
                            android:layout_width="wrap_content"
        
                            android:layout_height="wrap_content"
        
                            android:checked="true"
        
                            android:text="@string/setDate" />
        
                        <RadioButton
        
                            android:id="@+id/radioChooseTime"
        
                            android:layout_width="wrap_content"
        
                            android:layout_height="wrap_content"
        
                            android:layout_weight="1"
        
                            android:text="@string/setTime" />
        
                    </RadioGroup>
        
                    <DatePicker
        
                        android:id="@+id/datePicker"
        
                        android:layout_width="match_parent"
        
                        android:layout_height="match_parent" />
        
                    <TimePicker
        
                        android:id="@+id/timePicker"
        
                        android:layout_width="match_parent"
        
                        android:layout_height="match_parent"
        
                        android:visibility="invisible" />
        
                </LinearLayout>
        
                <LinearLayout
        
                    android:layout_width="match_parent"
        
                    android:layout_height="wrap_content"
        
                    android:background="#FDF5E6"
        
                    android:textAlignment="center" >
        
                    <Button
        
                        android:id="@+id/buttonReservationCompleted"
        
                        android:layout_width="wrap_content"
        
                        android:layout_height="wrap_content"
        
                        android:text="@string/setReservationCompleted" />
        
                    <TextView
        
                        android:id="@+id/textReservatedDateTime"
        
                        android:layout_width="wrap_content"
        
                        android:layout_height="wrap_content"
        
                        android:text="@string/none"
        
                        android:visibility="invisible" />
        
                </LinearLayout>
        
            </LinearLayout>
        
        </RelativeLayout>
    • res/values
      • 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>
신고
|  1  |  ···  |  17  |  18  |  19  |  20  |  21  |  22  |  23  |  24  |  25  |  ···  |  235  |