Android开发 Dialog对话框 DatePickerDialog

1. AlertDialog

AlertDialog是弹出的提醒对话框,有提示,确认,选择等功能。

没有公开的构造方法,一般用AlertDialog.Builder来完成参数设置,最后调用create方法创建。

参数设置常用的方法:

Android开发 Dialog对话框 DatePickerDialog_第1张图片

 代码:

package com.example.ch3;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class AlertDialogActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alert_dialog);

        findViewById(R.id.bt).setOnClickListener(this);
        tv = findViewById(R.id.tv);

    }

    @Override
    public void onClick(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Dear customer");
        builder.setMessage("Do you want to download this file?");
        builder.setNegativeButton("No, thanks", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                tv.setText("Cancel download file");
            }
        });
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                tv.setText("Downloading file...");
            }
        });

        AlertDialog dialog = builder.create();
        dialog.show();


    }
}

效果图:

Android开发 Dialog对话框 DatePickerDialog_第2张图片

 

2. DatePickerDialog

DatePickerDialog在 AlertDialog上添加了日期选择器DatePicker,达到日期选择弹窗的目的。

先来看一下日期选择器DatePicker的使用:




    

    

    
datePickerMode可以选"spinner"或者"calendar"
spinner同时显示日期选择滑动窗口和日历,可以用calendarViewShown关掉日历
calendar只显示日历

Android开发 Dialog对话框 DatePickerDialog_第3张图片

 java代码:

package com.example.ch3;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

public class DatePickerDialogActivity extends AppCompatActivity implements View.OnClickListener {

    private DatePicker dp;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_picker_dialog);

        findViewById(R.id.bt).setOnClickListener(this);

        dp = findViewById(R.id.dp);
        tv = findViewById(R.id.tv);
    }

    @Override
    public void onClick(View view) {
        String s = String.format("You choose  %d/%d/%d", dp.getYear(),dp.getMonth()+1,  dp.getDayOfMonth());
        tv.setText(s);
    }
}

效果图:

Android开发 Dialog对话框 DatePickerDialog_第4张图片

DatePickerDialog则是内置了日期选择器的弹窗,用onDateSet方法设置确定日期之后的动作。

代码:

package com.example.ch3;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

public class DatePickerDialogActivity extends AppCompatActivity implements View.OnClickListener, DatePickerDialog.OnDateSetListener {

    private DatePicker dp;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_picker_dialog);

        findViewById(R.id.bt).setOnClickListener(this);
        findViewById(R.id.bt_date).setOnClickListener(this);

        dp = findViewById(R.id.dp);
        tv = findViewById(R.id.tv);



    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.bt:
                String s = String.format("You choose  %d/%d/%d", dp.getYear(),dp.getMonth()+1,  dp.getDayOfMonth());
                tv.setText(s);
                break;
            case R.id.bt_date:
                DatePickerDialog datePickerDialog = new DatePickerDialog(this,this,2000,1,1);
                datePickerDialog.show();

                break;

        }

    }

    @Override
    public void onDateSet(DatePicker dp, int i, int i1, int i2) {
        String s = String.format("You choose  %d/%d/%d", dp.getYear(),dp.getMonth()+1,  dp.getDayOfMonth());
        tv.setText(s);
    }
}

 

效果图:

Android开发 Dialog对话框 DatePickerDialog_第5张图片

3.TimePickerDialog

TimePickerDialog和DatePickerDialog原理类似,内置了时间选择器TimePicker。

xml代码:




    

    



    

    

 

java代码:

package com.example.ch3;

import androidx.appcompat.app.AppCompatActivity;

import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;

import java.util.Calendar;

public class TimePickerActivity extends AppCompatActivity implements View.OnClickListener, TimePickerDialog.OnTimeSetListener {

    private TextView tv;
    private TimePicker tp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_picker);

        findViewById(R.id.bt).setOnClickListener(this);
        findViewById(R.id.bt_time).setOnClickListener(this);
        tv = findViewById(R.id.tv);
        tp = findViewById(R.id.tp);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt:
                String s = String.format("You choose %d/%d",tp.getHour(), tp.getMinute());
                tv.setText(s);
                break;
            case R.id.bt_time:
                Calendar calendar =Calendar.getInstance();
                TimePickerDialog timePickerDialog = new TimePickerDialog(this, this,
                        calendar.get(Calendar.HOUR_OF_DAY),
                        calendar.get(Calendar.MINUTE),
                        true);
                timePickerDialog.show();

        }
    }

    @Override
    public void onTimeSet(TimePicker timePicker, int i, int i1) {
        String s = String.format("You choose %d/%d",i, i1);
        tv.setText(s);
    }
}

效果图:

Android开发 Dialog对话框 DatePickerDialog_第6张图片

你可能感兴趣的:(APP开发,android,java,android,studio)