[android]代码库
1.MainActivity.java
package com.example.acer.myapplication;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
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.EditText;
import android.widget.TimePicker;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
Button dateDialog,timeDialog;
EditText dateText,timeText;
Calendar calendar = Calendar.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dateDialog = (Button) findViewById(R.id.dateDialog);
timeDialog = (Button) findViewById(R.id.timeDialog);
dateText = (EditText) findViewById(R.id.dateText);
timeText = (EditText) findViewById(R.id.timeText);
}
public void time(View v) {
new TimePickerDialog(MainActivity.this,new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
String timeInfo = "时间:"+hourOfDay+"时"+minute+"分";
timeText.setText(timeInfo);
}
},calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),true).show();
}
public void date(View v) {
new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String info = "日期:"+year + "年" + monthOfYear + "月" + dayOfMonth + "日";
dateText.setText(info);
}
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
}
}
2.activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/dateDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择日期"
android:onClick="date" />
<Button
android:id="@+id/timeDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择时间"
android:onClick="time" />
<EditText
android:id="@+id/dateText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/timeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>