android:id="@+id/tv_alert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"/>
java代码
public class AlertDialogActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tv_alert;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alert_dialog);
findViewById(R.id.btn_alert).setOnClickListener(this);
tv_alert = findViewById(R.id.tv_alert);
}
@Override
public void onClick(View view) {
//创建提醒对话框的建造器
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//设置对话框的标题文本
builder.setTitle("尊敬的用户");
//设置对话的内容文本
builder.setMessage("确定卸载?");
//设置对话框的肯定按钮文本及其监听器
builder.setPositiveButton("卸载",(dialog,which) -> {
tv_alert.setText("再见");
});
builder.setNegativeButton("再想想",(dialog,which) -> {
tv_alert.setText("留下来");
});
//根据建造器构建对话框
AlertDialog dialog = builder.create();
dialog.show();
}
}
二、日期对话框DatePickerDialog
日期选择器DatePicker可以让用户选择具体的年月日。
但DatePicker并非弹窗模式,而是在当前页面占据一块区域,并且不会自动关闭。
DatePickerDialog相当于在AlertDialog上装载了DatePicker,日期选择事件则由监听器OnDateSetListener负责响应,在该监听器的onDateSet方法中,开发者获取用户选择的具体日期再做后续处理。
第一种-点击选择日期出现日历
XML文件
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp">
java代码
public class DatePickerActivity extends AppCompatActivity implements View.OnClickListener, DatePickerDialog.OnDateSetListener {
private DatePicker dp_date;
private TextView tv_date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date_picker);
findViewById(R.id.btn_date).setOnClickListener(this);
tv_date = findViewById(R.id.tv_date);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_date:
//获取日历实例,里面包含了当前的年月日
Calendar calendar = Calendar.getInstance();
DatePickerDialog dialog = new DatePickerDialog(this,this,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
//显示日期对话框
dialog.show();
break;
}
}
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth) {
String desc = String.format("您选择的日期是%d年%d月%d日",year,month+1,dayOfMonth);
tv_date.setText(desc);
}
}
第二种-滚动选择日期
XML文件
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp">
android:id="@+id/dp_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:datePickerMode="spinner"
android:calendarViewShown="false"/>
java代码
public class DatePickerActivity extends AppCompatActivity implements View.OnClickListener {
private DatePicker dp_date;
private TextView tv_date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date_picker);
findViewById(R.id.btn_ok).setOnClickListener(this);
tv_date = findViewById(R.id.tv_date);
dp_date = findViewById(R.id.dp_date);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_ok:
String desc = String.format("您选择的日期是%d年%d月%d日",dp_date.getYear(),dp_date.getMonth()+1,dp_date.getDayOfMonth());
tv_date.setText(desc);
break;
}
}
}
三、时间对话框TimePickerDialog
时间选择器TimePicker可以让用户选择具体的小时和分钟。
TimePickerDialog用法类似DatePickerDialog。
方式一-出现时钟选择时间
XML文件
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp">
java代码
public class TimePickerActivity extends AppCompatActivity implements View.OnClickListener, TimePickerDialog.OnTimeSetListener {
private TextView tv_time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time_picker);
findViewById(R.id.btn_time).setOnClickListener(this);
tv_time = findViewById(R.id.tv_time);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_time:
//获取日历
Calendar calendar = Calendar.getInstance();
//构建时间话对话框
TimePickerDialog dialog = new TimePickerDialog(this,this,calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),true);
dialog.show();
break;
}
}
@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
String desc = String.format("您选择的日期是%d时%d分",hourOfDay,minute);
tv_time.setText(desc);
}
}
方式二-滚动选择时间
XML文件
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp">
android:id="@+id/tp_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:timePickerMode="spinner"/>
java代码
public class TimePickerActivity extends AppCompatActivity implements View.OnClickListener{
private TimePicker tp_time;
private TextView tv_time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time_picker);
findViewById(R.id.btn_ok).setOnClickListener(this);
tp_time = findViewById(R.id.tp_time);
tp_time.setIs24HourView(true);
tv_time = findViewById(R.id.tv_time);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_ok:
String desc = String.format("您选择的日期是%d时%d分",tp_time.getCurrentHour(),tp_time.getCurrentMinute());
tv_time.setText(desc);
break;
}
}
}
到此这篇关于Android对话框AlertDialog与DatePickerDialog及TimePickerDialog使用详解的文章就介绍到这了,更多相关Android AlertDialog内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章: