ITGenerations
2018_05_18 모바일 수업 날짜/시간 만들기 본문
<xml file>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn11"
android:text="날짜 선택"
android:onClick="click"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn22"
android:text="시간 선택"
android:onClick="click"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text11"
android:text="날짜:???"
android:textSize="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<java file>
package e.ryan.day12_ryan;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import java.util.Calendar;
/**
* Created by ryan on 2018. 5. 18..
*/
public class MainActivity2 extends AppCompatActivity{
Button btn11,btn22;
TextView textview;
String strDate="???";
String strTime="???";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
setTitle("날짜와 시간 대화 상자");
btn11 = (Button) findViewById(R.id.btn11);
btn22 = (Button) findViewById(R.id.btn22);
// text11 = (TextView) findViewById(R.id.text11);
// text22 = (TextView) findViewById(R.id.text22);
updateResult();
}
public void click(View view){
Calendar c = Calendar.getInstance();
switch (view.getId()){
case R.id.btn11:
c=Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int dayofMonth = c.get(Calendar.DAY_OF_MONTH);
new DatePickerDialog(this,mdatelistner,year,month,dayofMonth).show();
break;
case R.id.btn22:
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
new TimePickerDialog(this, mtimelistenr, hour,minute,true).show();
break;
}
}
DatePickerDialog.OnDateSetListener mdatelistner = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
strDate = String.format("%d년 %d월 %d일",year,month+1,dayOfMonth);
updateResult();
}
};
TimePickerDialog.OnTimeSetListener mtimelistenr = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
strTime = String.format("%d시 %d분",hourOfDay,minute);
updateResult();
}
};
public void updateResult(){
textview = (TextView) findViewById(R.id.text11);
textview.setText("날짜"+strDate +"\n시간"+strTime);
}
}
'Univ > 안드로이드' 카테고리의 다른 글
안드로이드 스튜디오 기말 대체: 미세먼지앱 만들기 (0) | 2018.06.08 |
---|---|
2018_05_25 sqlite 연동, 초기화, 입력, 조회 (0) | 2018.05.25 |
0504 수업내용 (0) | 2018.05.04 |
미세먼지/슈팅게임 앱 만들기 (0) | 2018.05.01 |
안드로이드 앱 만들기2 (0) | 2018.04.20 |