1. 提醒对话框(AlertDialog)
可以完成常见的交互操作,例如提示、确认、选择等功能。AlertDialog借助建造器AlertDialog.Builder才能完成参数设置。
调用建造器的create方法生成对话框实例,再调用对话框实例的show方法,在页面上弹出提醒对话框。
代码示例
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_alert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="弹出提醒对话框" />
<TextView
android:id="@+id/tv_alert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" />
</LinearLayout>
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 v) {
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("尊敬的用户");
builder.setMessage("您确定要退出吗?");
builder.setPositiveButton("确定", (dialogInterface, which) -> {
tv_alert.setText("退出成功");
});
builder.setNegativeButton("取消", (dialogInterface, which) -> {
tv_alert.setText("取消退出");
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
结果显示
2. 日期对话框DatePickerDialog
2.1 DatePicker
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<DatePicker
android:id="@+id/dp_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:calendarViewShown="false"
android:datePickerMode="spinner" />
<Button
android:id="@+id/btn_ok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="确定" />
<TextView
android:id="@+id/tv_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
public class DatePickerActivity extends AppCompatActivity implements View.OnClickListener {
private DatePicker dp_date;
private TextView tv_data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date_picker);
findViewById(R.id.btn_ok).setOnClickListener(this);
dp_date = findViewById(R.id.dp_date);
tv_data = findViewById(R.id.tv_data);
}
@Override
public void onClick(View view) {
if(view.getId() == R.id.btn_ok){
String desc= String.format("您选择的日期是%d年%d月%d日",
dp_date.getYear(),dp_date.getMonth()+1,dp_date.getDayOfMonth());
tv_data.setText(desc);
}
}
}
结果显示
2.2 DatePickerDialog
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选择日期"/>
<DatePicker
android:id="@+id/dp_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:calendarViewShown="false"
android:datePickerMode="spinner" />
<Button
android:id="@+id/btn_ok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="确定" />
<TextView
android:id="@+id/tv_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
public class DatePickerActivity extends AppCompatActivity implements View.OnClickListener, DatePickerDialog.OnDateSetListener {
private DatePicker dp_date;
private TextView tv_data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date_picker);
findViewById(R.id.btn_ok).setOnClickListener(this);
findViewById(R.id.btn_date).setOnClickListener(this);
dp_date = findViewById(R.id.dp_date);
tv_data = findViewById(R.id.tv_data);
}
@Override
public void onClick(View view) {
if(view.getId() == R.id.btn_ok){
String desc= String.format("您选择的日期是%d年%d月%d日",
dp_date.getYear(),dp_date.getMonth()+1,dp_date.getDayOfMonth());
tv_data.setText(desc);
}else if(view.getId() == R.id.btn_date){
DatePickerDialog dialog=new DatePickerDialog(this,this,2090,5,11 );
dialog.show();
}
}
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth) {
String desc= String.format("您选择的日期是%d年%d月%d日",
year,month+1,dayOfMonth);
tv_data.setText(desc);
}
}
结果显示
3. TimerPickerDialog
代码示例
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请选择时间"/>
<TimePicker
android:id="@+id/tp_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:timePickerMode="spinner" />
<Button
android:id="@+id/btn_ok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="确定" />
<TextView
android:id="@+id/tv_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
public class TimePickerActivity extends AppCompatActivity implements View.OnClickListener, TimePickerDialog.OnTimeSetListener {
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);
findViewById(R.id.btn_time).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 v){
if(v.getId() == R.id.btn_ok){
String desc= String.format("您选择的日期是%d时%d分",
tp_time.getHour(),tp_time.getMinute());
tv_time.setText(desc);
}else if(v.getId() == 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();
}
}
@Override
public void onTimeSet(TimePicker timePicker, int hourDay, int minute) {
String desc= String.format("您选择的日期是%d时%d分",
hourDay,minute);
tv_time.setText(desc);
}
}
结果显示