Day18-基于Android的AlertDialog、ProgressDialog、DatePickerDialog实例

本文介绍了如何在Android应用中使用AlertDialog、DatePickerDialog和动态管理Fragment,包括创建主页面MainActivity,分别展示对话框功能,并演示如何通过Fragment实现页面切换与功能整合。
摘要由CSDN通过智能技术生成

📢题目

🚑请创建一个模块,设计多个页面,分别用到AlertDialog、ProgressDialog、DatePickerDialog等对话框, 能够用到动态管理的碎片Fragment。

📢效果图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

🐱‍🚀一、开发主页面MainActivity

🎯1.编写主页布局activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/img"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="120dip"
        android:gravity="center_vertical|center_horizontal"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="综合应用案例"
            android:gravity="center"
            android:textColor="#00ff99"
            android:textSize="40sp" />
    </LinearLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#00ff00"
        android:text="AlertDialog" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="DatePickerDialog" />


</LinearLayout>

🎯2.编写主页MainActivity.java

package com.example.week6homework;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Button button1;
    private Button button2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = this.findViewById(R.id.button1);
        button2 = this.findViewById(R.id.button2);

        button1.getBackground().setAlpha(100);
        button2.getBackground().setAlpha(100);

        //为button1绑定跳转时间
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,AlertDialogand.class);
                startActivity(intent);
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,DatePickerDialog.class);
                startActivity(intent);
            }
        });
    }
}

🐱‍🚀二、开发AlertDialog页面

🎯1.编写activity_alert_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/img"
    tools:context=".AlertDialogand">

  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="120dip"
      android:gravity="center_vertical|center_horizontal"
      android:orientation="vertical"
      tools:ignore="MissingConstraints">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="AlertDialog案例"
        android:gravity="center"
        android:textColor="#00ff99"
        android:textSize="40sp" />
  </LinearLayout>

  <Button
      android:id="@+id/button1"
      android:layout_width="match_parent"
      android:layout_height="100dp"
      android:background="#00ff00"
      android:text="AlertDialog"
      app:layout_constraintBottom_toTopOf="@+id/button2"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintHorizontal_bias="0.0"
      app:layout_constraintStart_toStartOf="parent"
      tools:ignore="MissingConstraints" />

  <Button
      android:id="@+id/button2"
      android:layout_width="match_parent"
      android:layout_height="100dp"
      android:text="列表对话框"
      app:layout_constraintBottom_toTopOf="@+id/button3"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintHorizontal_bias="0.0"
      app:layout_constraintStart_toStartOf="parent"
      tools:ignore="MissingConstraints" />

  <Button
      android:id="@+id/button3"
      android:layout_width="match_parent"
      android:layout_height="100dp"
      android:layout_marginBottom="144dp"
      android:text="进度条对话框"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintHorizontal_bias="0.0"
      app:layout_constraintStart_toStartOf="parent"
      tools:ignore="MissingConstraints" />

</androidx.constraintlayout.widget.ConstraintLayout>

🎯2.编写AlertDialog.java

package com.example.week6homework;


import java.util.Timer;
import java.util.TimerTask;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class AlertDialogand extends AppCompatActivity {
    Button alert,list,progress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alert_dialog);
        alert = (Button)findViewById(R.id.button1);
        list = (Button)findViewById(R.id.button2);
        progress = (Button)findViewById(R.id.button3);

        alert.getBackground().setAlpha(100);
        list.getBackground().setAlpha(100);
        progress.getBackground().setAlpha(100);
        //绑定事件
        alert.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                createAlertDialog().show();
            }

        });
        list.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
//                createListDialog().show();
                createListDialog().show();
            }

        });
        progress.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                createProgressDialog().show();
            }

        });

    }

    /**
     * 生成信息交互对话框!
     * @return
     */
    private AlertDialog createAlertDialog(){
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setIcon(R.drawable.img)//设置标题的图片
                .setTitle("提示")//设置对话框的标题
                .setMessage("点击确定返回主页")//设置对话框的内容
                //设置对话框的按钮
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        AlertDialogand.this.finish();
                    }
                }).create();
        return dialog;
    }

    private AlertDialog createListDialog(){
        final String items[] = {"吃饭", "睡觉", "打豆豆", "摆烂"};
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setIcon(R.drawable.img)//设置标题的图片
                .setTitle("选择您的兴趣爱好:")//设置对话框的标题
                .setSingleChoiceItems(items,-1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(AlertDialogand.this, "您的选择:"+items[which], Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).create();
        return dialog;

    }

    private ProgressDialog createProgressDialog(){
        final ProgressDialog dialog = new ProgressDialog(this);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setMessage("正在加载中");
        dialog.setMax(100);
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int currentProgress = 0;

            @Override
            public void run() {
                currentProgress+=10;
                dialog.setProgress(currentProgress);
                if (currentProgress>=100) {
                    timer.cancel();
                    dialog.dismiss();
                }
            }
        }, 0, 1000);
        return dialog;
    }
}

🐱‍🚀三、开发DatePickerDialog页面

🎯1.编写activity_date_picker_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:background="@drawable/img"
    tools:context=".DatePickerDialog">

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="120dip"
        android:gravity="center_vertical|center_horizontal"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="0dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:text="DatePicker应用案例"
            android:textColor="#00ff99"
            android:textSize="40dp" />

    </LinearLayout>

    <TextView
        android:id="@+id/c57DataTimeTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text=""
        android:textSize="18sp" />

    <ScrollView
        android:id="@+id/c57scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

        </LinearLayout>
    </ScrollView>

    <DatePicker
        android:id="@+id/c57datePicker1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffff"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout2"
        tools:layout_editor_absoluteX="0dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

🎯2. 编写DatePickerDialog.java

package com.example.week6homework;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class DatePickerDialog extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿杰杰杰のblog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值