代码
MainActivity.java
package com.example.me.myapplication;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
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.TextView;
import android.widget.TimePicker;
public class MainActivity extends AppCompatActivity{
Button btn1, btn2, btn3;
TextView textDate, textTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//监听按钮,绑定点击事件
btn1 = (Button)findViewById(R.id.button1);
btn2 = (Button)findViewById(R.id.button2);
btn3 = (Button)findViewById(R.id.button3);
btn1.setOnClickListener(new mClick());
btn2.setOnClickListener(new mClick());
btn3.setOnClickListener(new mClick());
textDate = (TextView)findViewById(R.id.txtDate);
textTime = (TextView)findViewById(R.id.txtTime);
}
class mClick implements View.OnClickListener {
int m_year = 2012,
m_month = 1,
m_day = 1,
m_hour = 12,
m_minute = 1;
@Override
//按钮点击事件
public void onClick(View v){
if(v == btn1){
//“进度对话框”按钮
ProgressDialog d = new ProgressDialog(MainActivity.this);
d.setTitle("进度对话框");
d.setIndeterminate(true);
d.setMessage("程序正在Loading...");
d.setCancelable(true);
d.setMax(10);
d.show();
}else if (v == btn2){
//“日期对话框”按钮的点击处理
DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() {
@Override
//设置日期事件
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
m_year = year;
m_month = month;
m_day = dayOfMonth;
textDate.setText("设置的日期为" + m_year + "年" + m_month + "月" + m_day + "日");
}
};
DatePickerDialog date = new DatePickerDialog(MainActivity.this, dateListener, m_year, m_month, m_day);
date.setTitle("日期对话框");
date.show();
}else if (v == btn3){
//“时间对话框”按钮的点击处理
TimePickerDialog.OnTimeSetListener timeListener = new TimePickerDialog.OnTimeSetListener() {
@Override
//设置时间事件
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
m_hour = hourOfDay;
m_minute = minute;
textTime.setText("设置时间为" + hourOfDay + ":" + m_minute);
}
};
TimePickerDialog d = new TimePickerDialog(MainActivity.this, timeListener, m_hour, m_minute, true);
d.setTitle("时间对话框");
d.show();
}
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:text="进度对话框"
android:textSize="20sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:text="日期对话框"
android:textSize="20sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button3"
android:text="时间对话框"
android:textSize="20sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtDate"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtTime"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
执行结果