一、实验要求
进度、日期、时间对话框示例,DatePickerDialog和时间选择对话框TimePickerDialog都继承于AlertDialog,一般用于日期和时间的设定
二、让我们先来看一下运行结果,方便我们清楚实验需求
三、源码分析(见注释)
3.1 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="进度对话框"
android:textSize="20sp"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="日期对话框"
android:textSize="20sp"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="时间对话框"
android:textSize="20sp"/>
</LinearLayout>
3.2 控制文件
package hp480.example.ex3_6;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
public class ex3_6 extends Activity
{
Button btn1,btn2,btn3;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ex3_6);
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());
}
class mClick implements OnClickListener
{
int m_year = 2012;
int m_month = 1;
int m_day = 1;
int m_hour = 12, m_minute = 1;
@Override
public void onClick(View v)
{
if(v == btn1)
{
ProgressDialog d=new ProgressDialog (ex3_6.this);
d.setTitle("进度对话框");
d.setIndeterminate(true);
d.setMessage("程序正在Loading...");
d.setCancelable(true);
d.setMax(10);
d.show();
}
else if(v == btn2)
{
//设置日期监听器
OnDateSetListener dateListener = new OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth)
{
m_year = year;
m_month = monthOfYear;
m_day = dayOfMonth;
}
};
//创建日期对话框对象
DatePickerDialog date = new DatePickerDialog(ex3_6.this,
dateListener, m_year, m_month, m_day);
date.setTitle("日期对话框");
date.show();
}
else if(v == btn3)
{ //设置时间监听器
OnTimeSetListener timeListener = new OnTimeSetListener()
{
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
m_hour = hourOfDay;
m_minute = minute;
}
};
TimePickerDialog d = new TimePickerDialog(ex3_6.this,
timeListener, m_hour, m_minute, true);
d.setTitle("时间对话框");
d.show();
}
}
}
}