Android_进度及日期、时间对话框应用

代码

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




    

        

执行结果

Android_进度及日期、时间对话框应用_第1张图片
Android_进度及日期、时间对话框应用_第2张图片
Android_进度及日期、时间对话框应用_第3张图片
Android_进度及日期、时间对话框应用_第4张图片

你可能感兴趣的:(Android_进度及日期、时间对话框应用)