1. AlertDialog
AlertDialog是弹出的提醒对话框,有提示,确认,选择等功能。
没有公开的构造方法,一般用AlertDialog.Builder来完成参数设置,最后调用create方法创建。
参数设置常用的方法:
代码:
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();
}
}
效果图:
2. DatePickerDialog
DatePickerDialog在 AlertDialog上添加了日期选择器DatePicker,达到日期选择弹窗的目的。
先来看一下日期选择器DatePicker的使用:
<?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"
tools:context=".DatePickerDialogActivity"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<DatePicker
android:id="@+id/dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="spinner"
android:calendarViewShown="false"></DatePicker>
<Button
android:id="@+id/bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Confirm"></Button>
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></TextView>
</LinearLayout>
datePickerMode可以选"spinner"或者"calendar" spinner同时显示日期选择滑动窗口和日历,可以用calendarViewShown关掉日历 calendar只显示日历
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);
}
}
效果图:
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);
}
}
效果图:
3.TimePickerDialog
TimePickerDialog和DatePickerDialog原理类似,内置了时间选择器TimePicker。
xml代码:
<?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"
tools:context=".DatePickerDialogActivity"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<TimePicker
android:id="@+id/tp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="spinner"
></TimePicker>
<Button
android:id="@+id/bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Confirm"></Button>
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></TextView>
<Button
android:id="@+id/bt_time"
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Choose Time"></Button>
</LinearLayout>
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);
}
}
效果图:文章来源:https://www.toymoban.com/news/detail-408219.html
文章来源地址https://www.toymoban.com/news/detail-408219.html
到了这里,关于Android开发 Dialog对话框 DatePickerDialog的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!