一、怎么利用Button来查看日期,代码如下:
布局代码:
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取日期"
android:onClick="getDate"
/>
</LinearLayout>
java代码:
package com.zking.laci.android05;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import java.util.Calendar;
/**
* Created by Laci on 2017/6/4.
*/
public class TimeActivity extends AppCompatActivity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time);
}
public void getDate(View view){
//获取当前时间
Calendar c=Calendar.getInstance();
//年
int year=c.get(Calendar.YEAR);
//月
int month=c.get(Calendar.MONTH);
//日
int day=c.get(Calendar.DAY_OF_MONTH);
//弹出时间对话框
DatePickerDialog dpd=new DatePickerDialog(this,new DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Log.i("test","时间"+year+":"+month+":"+dayOfMonth);
}
},year,moth,day);
dpd.show();
}
}