Android开发 day08(对话框)

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);

    }
}

结果显示

 原视频

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值