java源代码
package ytu.it.a201758501137.widgetdemo;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.icu.text.SimpleDateFormat;
import android.icu.util.GregorianCalendar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
public class DateTimePickerActivity extends AppCompatActivity {
NenoTextview txt_name;
Button button1,button2,coun;
TextView tex1,tex2,tex3;
int year1,month1,day1;
int year2,month2,day2;
String date1,date2,date3,date4;
String date5,date6,date7,date8;
StringBuilder str = new StringBuilder("");
StringBuilder str1 = new StringBuilder("");
int Hour1,Minutes1;
int Hour2,Minutes2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date_time_picker);
txt_name= (NenoTextview) findViewById(R.id.dptext);
/*datePicker = (DatePicker) findViewById(R.id.dpPicker);*/
button1= (Button) findViewById(R.id.dpb1);
button2= (Button) findViewById(R.id.dpb2);
coun= (Button) findViewById(R.id.coun);
tex1= (TextView) findViewById(R.id.datetext1);
tex2= (TextView) findViewById(R.id.datetext2);
tex3= (TextView) findViewById(R.id.lef);
button1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
DatePickerDialog datePickerDialog=new DatePickerDialog(DateTimePickerActivity.this,DatePickerDialog.THEME_HOLO_LIGHT);
datePickerDialog.setOnDateSetListener(new DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
str.append(i+"年"+i1+"月"+i2+"日"+" ");
Calendar time = Calendar.getInstance();
TimePickerDialog timePickerDialog=new TimePickerDialog(DateTimePickerActivity.this, TimePickerDialog.THEME_HOLO_LIGHT, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int i, int i1) {
str.append("时间为"+i+":"+i1);
DateTimePickerActivity.this.Hour1=i;
DateTimePickerActivity.this.Minutes1=i1;
tex1.setText(str);
}
},time.get(Calendar.HOUR_OF_DAY), time
.get(Calendar.MINUTE),true);
timePickerDialog.setTitle("请选择开始时间");
timePickerDialog.show();
i1+=1;
year1=i;
month1=i1;
day1=i2;
if(month1<10){
date2=String.valueOf(month1);
date2="0"+date2;
}else{
date2=String.valueOf(month1);
}
if(day1<10){
date3=String.valueOf(day1);
date3="0"+date3;
}else{
date3=String.valueOf(day1);
}
date1=String.valueOf(year1);
date4=date1+date2+date3;
}
});
datePickerDialog.setTitle("请选择开始日期");
datePickerDialog.show();
}
});
button2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
DatePickerDialog datePickerDialog=new DatePickerDialog(DateTimePickerActivity.this,DatePickerDialog.THEME_HOLO_LIGHT);
datePickerDialog.setOnDateSetListener(new DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
str1.append(i+"年"+i1+"月"+i2+"日"+" ");
Calendar time = Calendar.getInstance();
TimePickerDialog timePickerDialog=new TimePickerDialog(DateTimePickerActivity.this, TimePickerDialog.THEME_HOLO_LIGHT, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int i, int i1) {
str1.append("时间为"+i+":"+i1);
DateTimePickerActivity.this.Hour2=i;
DateTimePickerActivity.this.Minutes2=i1;
tex2.setText(str1);
}
},time.get(Calendar.HOUR_OF_DAY), time
.get(Calendar.MINUTE),true);
timePickerDialog.setTitle("请选择开始时间");
timePickerDialog.show();
i1+=1;
tex2.setText(i+"年"+i1+"月"+i2+"日");
year2=i;
month2=i1;
day2=i2;
if(month2<10){
date6=String.valueOf(month2);
date6="0"+date6;
}else{
date6=String.valueOf(month2);
}
if(day2<10){
date7=String.valueOf(day2);
date7="0"+date7;
}else{
date7=String.valueOf(day2);
}
date5=String.valueOf(year2);
date8=date5+date6+date7;
}
});
datePickerDialog.setTitle("请选择结束日期");
datePickerDialog.show();
}
});
coun.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
Date date11=null,date22=null;
try {
date11 = ft.parse(date4);
date22 = ft.parse(date8);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal1=Calendar.getInstance();
Calendar cal2=Calendar.getInstance();
cal1.setTime(date11);
cal2.setTime(date22);
long starttime=cal1.getTimeInMillis();
long endtime=cal2.getTimeInMillis();
long numberOfDays = (endtime - starttime)/(24 * 60 * 60 * 1000);
tex3.setText(numberOfDays+"");
}
});
}
}
xml文件源代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/dpbk"
android:orientation="vertical"
tools:context="ytu.it.a201758501137.widgetdemo.DateTimePickerActivity">
<ytu.it.a201758501137.widgetdemo.NenoTextview
android:id="@+id/dptext"
android:layout_width="wrap_content"
android:text="小时光倒计时"
android:textSize="30dp"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content" />
<Button
android:id="@+id/dpb1"
android:textColor="#ffff"
android:layout_below="@+id/dptext"
android:layout_marginTop="20dp"
android:text="请选择开始日期/时间"
android:background="@drawable/buttonbackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/dpb2"
android:textColor="#ffff"
android:layout_below="@+id/dpb1"
android:layout_marginTop="20dp"
android:text="请选择结束日期/时间"
android:background="@drawable/buttonbackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/datetext1"
android:textColor="#ffff"
android:layout_marginLeft="20dp"
android:layout_below="@+id/dptext"
android:layout_marginTop="35dp"
android:text=""
android:layout_toRightOf="@+id/dpb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/datetext2"
android:textColor="#ffff"
android:layout_marginLeft="20dp"
android:layout_below="@+id/dptext"
android:layout_marginTop="105dp"
android:text=""
android:layout_toRightOf="@+id/dpb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/lef"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00"
android:layout_below="@id/datetext2"
android:textColor="#ffff"
android:textSize="70dp"
android:layout_marginTop="170dp"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/datetext2"
android:text="还有"
android:textSize="30dp"
android:textColor="#ffff"
android:layout_marginTop="195dp"
android:layout_marginRight="20dp"
android:layout_toLeftOf="@+id/lef"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/datetext2"
android:text="天"
android:textSize="30dp"
android:textColor="#ffff"
android:layout_marginTop="195dp"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@+id/lef"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/coun"
android:text="看看还有几天"
android:textColor="#ffff"
android:layout_below="@+id/datetext2"
android:layout_marginTop="50dp"
android:background="@drawable/buttonbackground"
android:layout_marginLeft="140dp"/>
</RelativeLayout>