날짜 시간 위젯

Posted by PeEn
2019. 10. 21. 15:35 Programing/Android

1. activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:baselineAligned="false"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <Chronometer
            android:id="@+id/chronometer1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#CCCCCC"
            android:format=" 예약에 걸린 시간  %s "
            android:gravity="center"
            android:textSize="20dp" />
    </LinearLayout>

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/rdoCal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="날짜 설정" />

        <RadioButton
            android:id="@+id/rdoTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="시간 설정" />
    </RadioGroup>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center" >

            <DatePicker
                android:datePickerMode="spinner"
                android:id="@+id/datePicker1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center" />

            <TimePicker
                android:timePickerMode="spinner"
                android:id="@+id/timePicker1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center" />
        </FrameLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#CCCCCC"
        android:gravity="center" >

        <TextView
            android:id="@+id/tvYear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0000" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="년" />

        <TextView
            android:id="@+id/tvMonth"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="00" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="월" />

        <TextView
            android:id="@+id/tvDay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="00" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="일" />

        <TextView
            android:id="@+id/tvHour"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="00" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="시" />

        <TextView
            android:id="@+id/tvMinute"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="00" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="분 예약됨" />
    </LinearLayout>

</LinearLayout>

2. MainActivity.java

	Chronometer chrono;
	RadioButton rdoCal, rdoTime;
	DatePicker dPicker;
	TimePicker tPicker;
	TextView tvYear, tvMonth, tvDay, tvHour, tvMinute;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		setTitle("시간 예약");

		// 크로노미터
		chrono = (Chronometer) findViewById(R.id.chronometer1);

		// 라디오버튼 2개
		rdoCal = (RadioButton) findViewById(R.id.rdoCal);
		rdoTime = (RadioButton) findViewById(R.id.rdoTime);

		// FrameLayout의 2개 위젯
		dPicker = (DatePicker) findViewById(R.id.datePicker1);
		tPicker = (TimePicker) findViewById(R.id.timePicker1);

		// 텍스트뷰 중에서 연,월,일,시,분 숫자
		tvYear = (TextView) findViewById(R.id.tvYear);
		tvMonth = (TextView) findViewById(R.id.tvMonth);
		tvDay = (TextView) findViewById(R.id.tvDay);
		tvHour = (TextView) findViewById(R.id.tvHour);
		tvMinute = (TextView) findViewById(R.id.tvMinute);

		// 처음에는 2개를 안보이게 설정
		rdoCal.setVisibility(View.INVISIBLE);
		rdoTime.setVisibility(View.INVISIBLE);
		tPicker.setVisibility(View.INVISIBLE);
		dPicker.setVisibility(View.INVISIBLE);

		rdoCal.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				tPicker.setVisibility(View.INVISIBLE);
				dPicker.setVisibility(View.VISIBLE);
			}
		});

		rdoTime.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				tPicker.setVisibility(View.VISIBLE);
				dPicker.setVisibility(View.INVISIBLE);
			}
		});

		// 크로노미터를 클릭하면..타이머 시작
		chrono.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				chrono.setBase(SystemClock.elapsedRealtime());
				chrono.start();
				chrono.setTextColor(Color.RED);
				rdoCal.setVisibility(View.VISIBLE);
				rdoTime.setVisibility(View.VISIBLE);
			}
		});

		// 크로노미터를 롱클릭하면..타이머 중지
		tvYear.setOnLongClickListener(new View.OnLongClickListener() {
			public boolean onLongClick(View v) {
				chrono.stop();
				chrono.setTextColor(Color.BLUE);

				tvYear.setText(Integer.toString(dPicker.getYear()));
				tvMonth.setText(Integer.toString(1 + dPicker.getMonth()));
				tvDay.setText(Integer.toString(dPicker.getDayOfMonth()));

				tvHour.setText(Integer.toString(tPicker.getCurrentHour()));
				tvMinute.setText(Integer.toString(tPicker.getCurrentMinute()));

				rdoCal.setVisibility(View.INVISIBLE);
				rdoTime.setVisibility(View.INVISIBLE);
				tPicker.setVisibility(View.INVISIBLE);
				dPicker.setVisibility(View.INVISIBLE);
				return false;

			}
		});

	}

}

'Programing > Android' 카테고리의 다른 글

커스텀 토스트, 커스텀 다이얼로그  (0) 2019.10.23
배열과 테이블 위젯 이용한 계산기, 숫자버튼 예제  (0) 2019.10.21
탭호스트  (0) 2019.10.21
ContextMenu  (0) 2019.10.21
Android LineChart  (0) 2019.10.16