달력

5

« 2024/5 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
728x90
반응형

android - 시간위젯으로 시간입력받기 - 시간변경 이벤트 처리

안녕하세요?
프쟁이 입니다.

이 샘플소스는 안드로이드 설치폴더에 android-sdk-windows/samples/android-8/ApiDemos 위치에 원본소스가 있습니다.

시간입력 위젯을 액티비티에 출력하여, 위젯의 시간값변경 이벤트가

발생할때마다, 텍스트뷰에 그 변경된 값을 출력하는 처리를 합니다.

먼저 원하시는 패키지 경로에 DateWidgets2.java 액티비티 클래스 파일을 추가합니다.

/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.korsoft.Test023;

import net.korsoft.Test023.R;

import android.app.Activity;
import android.widget.TextView;
import android.widget.TimePicker;
import android.os.Bundle;

/*
* 시간입력 위젯을 액티비티에 출력하여, 위젯의 시간값변경 이벤트가
* 발생할때마다, 텍스트뷰에 그 변경된 값을 출력하는 처리를 합니다.
*/
public class DateWidgets2 extends Activity {

// where we display the selected date and time
private TextView mTimeDisplay;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.date_widgets_example_2);

/*
* TimePicker 위젯 컨트롤을 레이아웃 xml 에서 객체로 가져옵니다.
*/
TimePicker timePicker = (TimePicker) findViewById(R.id.timePicker);
timePicker.setCurrentHour(12);
timePicker.setCurrentMinute(15);

mTimeDisplay = (TextView) findViewById(R.id.dateDisplay);

updateDisplay(12, 15);

/*
* 시간입력 위젯의 시간값 변경이벤트 리스너에 onTimeChanged 메소드를 구현하여
* 시간값이 변경될때마다 화면의 텍스트 뷰에 시간값을 갱신하는 처리를 해줍니다.
*/
timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
updateDisplay(hourOfDay, minute);
}
});
}

/*
* 텍스트뷰에 시간값을 갱신해주는 메소드입니다.
*/
private void updateDisplay(int hourOfDay, int minute) {
mTimeDisplay.setText(
new StringBuilder()
.append(pad(hourOfDay)).append(":")
.append(pad(minute)));
}

private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}

}

레이아웃 xml 파일인 /res/layout/date_widgets_example_2.xml 파일을 추가해줍니다.

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<!-- example of using the time changed callback, with now visible 'set' button-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<!-- 시간입력 위젯 -->
<TimePicker android:id="@+id/timePicker"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>

<TextView android:id="@+id/dateDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="4dip"
android:text="@string/date_widgets_example_dateDisplay_text"/>

</LinearLayout>


문자열 리소스 파일인, /res/values/strings.xml 파일을 설정합니다.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Test023Activity!</string>
<string name="app_name">Test023</string>
<string name="date_widgets_example_dateDisplay_text"></string>
<string name="date_widgets_example_pickTime_text">change the time</string>
<string name="date_widgets_example_pickDate_text">change the date</string>
</resources>

AndroidManifest.xml 파일을 설정합니다.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.korsoft.Test023"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".DateWidgets2" android:label="Views/Date Widgets/2. Inline">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
728x90
반응형
:
Posted by mapagilove