«   2024/12   »
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
Tags
more
Archives
Today
Total
관리 메뉴

ITGenerations

2018_05_18 모바일 수업 날짜/시간 만들기 본문

Univ/안드로이드

2018_05_18 모바일 수업 날짜/시간 만들기

ITGenerations 2018. 5. 18. 15:45

<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);
}

}