Android 对话框、进度条、定时器

Android 对话框、进度条、定时器

第一步:在xml文件上界面布局
采用LinearLayout布局,并添加ImageView控件显示一张图片,当进度条走到0%时图片出现感叹号。点击图片弹出对话框,点击修复按钮。使进度条走到100%,感叹号消失。代码如下:

<?xml version="1.0" encoding="utf-8"?>
<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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/i1"
        android:layout_width="100dp"
        android:layout_height="150dp"
        android:src="@mipmap/b2"
        android:layout_gravity="center" />
</LinearLayout>

界面截图:
在这里插入图片描述
第二步:在对话框xml文件上界面布局
采用LinearLayout布局,添加进度条控件ProgressBar和TextView控件显示当前的进度,添加Button修复按钮,监听事件,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="200dp"
    android:layout_height="200dp">
    <ProgressBar
        android:id="@+id/p1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20dp"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:layout_marginTop="20dp"
        android:max="100"
        android:progress="100"/>
    <TextView
        android:id="@+id/t1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="100%"/>

    <Button
	    android:id="@+id/b1"
	    android:layout_width="100dp"
	    android:layout_height="wrap_content"
	    android:layout_gravity="center"
	    android:text="维修"/>

</LinearLayout>

界面截图:
在这里插入图片描述
第三步:功能的实现
找到id并绑定
在这里插入图片描述
定义对话框

public class MyDialog extends Dialog
    {
    
    
    }

鼠标移至Dialog,键盘按下Alt+Enter,选择第一个,按ok

    public class MyDialog extends Dialog
    {
    
    
        public MyDialog(@NonNull Context context) {
    
    
            super(context);
        }
    }

找到对应xml文件和找到对应的控件并绑定ID,最后添加按钮的监听事件
在这里插入图片描述

    public class MyDialog extends Dialog
    {
    
    
        public MyDialog(@NonNull Context context) {
    
    
            super(context);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
            p1=(ProgressBar)findViewById(R.id.p1);
            b1=(Button)findViewById(R.id.b1);
            t1=(TextView)findViewById(R.id.t1);
            b1.setOnClickListener(new View.OnClickListener() {
    
    
                @Override
                public void onClick(View view) {
    
    

                }
            });

        }
    }

第四步:点击图片弹出对话框
在这里插入图片描述

第五步:定时器功能的实现
首先定义Handler、Handler、进度条的最大值
在这里插入图片描述
定义定时器的启动

public void startTimer()//定时器
    {
    
    
        timer=new Timer();
        timer.schedule(new TimerTask() {
    
    
            @Override
            public void run() {
    
    
                handler.sendEmptyMessage(0x22);
            }
        },0,1000);//1秒启动一次
    }

定义定时器的停止

private void stopTimer(){
    
    
        if (timer != null) {
    
    
            timer.cancel();
            timer = null;
        }
    }

定时器启动

    public void startFlicker()//启动更新
    {
    
    
        handler=new Handler()
        {
    
    
            @Override
            public void handleMessage(@NonNull Message msg) {
    
    
                if(msg.what==0x22)
                {
    
    
                    i--;//进度1秒1秒的减少
                    if(i<=0)//进度为0时
                    {
    
    
                        stopTimer();//停止定时器
                        i1.setImageResource(R.mipmap.b1);//图片更改,出现感叹号
                    }
                    else if(i>0)//如果不为0
                    {
    
    
                        i1.setImageResource(R.mipmap.b2);//图片不出现感叹号
                    }
                }
                super.handleMessage(msg);

            }
        };
        startTimer();//启动定时器
    }

启动更新
在这里插入图片描述
在这里插入图片描述

 b1.setOnClickListener(new View.OnClickListener() {
    
    
                @Override
                public void onClick(View view) {
    
    //点击修复
                    i=100;//进度条为100%
                    stopTimer();//停止定时器
                    p1.setProgress(i);//更新进度
          		    t1.setText(i+"%");//显示当前进度
                    startFlicker();//启动更新
                }
            });
             p1.setProgress(i);
		     t1.setText(i+"%");

以下是本项目的源代码链接:
https://download.csdn.net/download/Scxioi0/12911444

猜你喜欢

转载自blog.csdn.net/Scxioi0/article/details/108923128