Android Development - Notification

popup dialog

activity_main.xml

<?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:gravity="center">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_1"
        android:text="升级"/>

</LinearLayout>

 MianActivity.java

package com.example.notification;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.TouchDelegate;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

// TODO 通知
// TODO 1、弹出式对话框
public class MainActivity extends AppCompatActivity {
    Button btn_1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_1=findViewById(R.id.btn_1);
        btn_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //1、创建对话框的构建器(方框)
                AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
                //2、设置弹出框
                builder.setTitle("版本更新");
                builder.setMessage("检查到新版本,是否立即更新");
                // TODO 积极的操作(确定)
                builder.setPositiveButton("立即升级", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this, "立即升级", Toast.LENGTH_SHORT).show();
                    }
                });
                // TODO 消极的操作(取消)
                builder.setNegativeButton("下次再说", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this, "下次再说", Toast.LENGTH_SHORT).show();
                    }
                });
                //3、显示对话框
                builder.show();
            }
        });
    }
}

running result:

 popup dialog

activity_main.xml

<?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:gravity="center">

    <Button
        android:id="@+id/btn_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="更多" />
</LinearLayout>

UI effects:

 MainActivity.java

package com.example.notification;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.TouchDelegate;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

// TODO 通知
public class MainActivity extends AppCompatActivity {
    Button btn_2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_2=findViewById(R.id.btn_2);
      
        // TODO 2、弹出框
        btn_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("更多");
                String[] data = {"上一首","下一首","暂停播放","分享","收藏"};
                builder.setItems(data, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this, data[i], Toast.LENGTH_SHORT).show();
                    }
                });
                builder.show();
            }
        });
    }
}

running result:

 progress bar popup

// TODO 3、进度弹出对话框
        btn_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ProgressDialog dialog =new ProgressDialog(MainActivity.this);
                dialog.setTitle("正在加载");
                dialog.setMessage("正在加载中,请稍后.....");
                dialog.show();
            }
        });

running result:

 

Simulate loading status

1. The progress of the circle style

 // TODO 4、模拟下载过程
        btn_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ProgressDialog dialog = new ProgressDialog(MainActivity.this);
                dialog.setTitle("下载");
                // TODO 设置进度条的样式
                //转圈进度样式
                dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                dialog.show();

            }
        });

running result:

2. Percentage style progress 

 // TODO 4、模拟下载过程
        btn_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ProgressDialog dialog = new ProgressDialog(MainActivity.this);
                dialog.setTitle("下载");
                // TODO 设置进度条的样式
                //转圈进度样式
//                dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                //百分比进度样式(0%--------100%)
                dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                dialog.show();

                // TODO 创建子线程用于更新当前进度
                new Thread(){
                    @Override
                    public void run() {
                        for (int i=1; i<=100; i++){
                            dialog.setProgress(i);
                            try {
                                Thread.sleep(100);
                            }catch (InterruptedException e){
                                throw new RuntimeException(e);
                            }
                        }
                        //关闭弹出框
                        dialog.dismiss();
                    }
                }.start();
            }
        });

running result:

 custom dialog

1. Create a custom layout page file in the layout folder

Right-click layout -> new -> Layout Resource File

 2. Write the mylogin.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300sp"
    android:layout_height="160sp"
    android:orientation="vertical"
    android:padding="10sp"
    android:gravity="center">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_username"
        android:hint="请输入账号"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_pwd"
        android:hint="请输入密码"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_login"
        android:text="登录"/>

</LinearLayout>

3. Write Java code

 // TODO 5、自定义样式弹出框
        btn_5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //创建弹出框
                Dialog dialog=new Dialog(MainActivity.this);
                dialog.setTitle("登录");
                //设置自定义的布局页面mylogin.xml文件加载到对话框里
                dialog.setContentView(R.layout.mylogin);
                dialog.show();
            }
        });

running result:

full code

configuration file

Activity_main.xml

<?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"
    android:gravity="center">

    <Button
        android:id="@+id/btn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="升级" />
    <Button
        android:id="@+id/btn_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="更多" />
    <Button
        android:id="@+id/btn_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="加载" />
    <Button
        android:id="@+id/btn_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下载" />
    <Button
        android:id="@+id/btn_5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录" />
</LinearLayout>

java code

package com.example.notification;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.TouchDelegate;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

// TODO 通知
public class MainActivity extends AppCompatActivity {
    Button btn_1;
    Button btn_2;
    Button btn_3;
    Button btn_4;
    Button btn_5;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_1=findViewById(R.id.btn_1);
        btn_2=findViewById(R.id.btn_2);
        btn_3=findViewById(R.id.btn_3);
        btn_4=findViewById(R.id.btn_4);
        btn_5=findViewById(R.id.btn_5);
        // TODO 1、弹出式对话框
        btn_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //1、创建对话框的构建器(方框)
                AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
                //2、设置弹出框
                builder.setTitle("版本更新");
                builder.setMessage("检查到新版本,是否立即更新");
                // TODO 积极的操作(确定)
                builder.setPositiveButton("立即升级", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this, "立即升级", Toast.LENGTH_SHORT).show();
                    }
                });
                // TODO 消极的操作(取消)
                builder.setNegativeButton("下次再说", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this, "下次再说", Toast.LENGTH_SHORT).show();
                    }
                });
                //3、显示对话框
                builder.show();
            }
        });
        // TODO 2、弹出对话框
        btn_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("更多");
                String[] data = {"上一首","下一首","暂停播放","分享","收藏"};
                builder.setItems(data, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this, data[i], Toast.LENGTH_SHORT).show();
                    }
                });
                builder.show();
            }
        });
        // TODO 3、进度弹出对话框
        btn_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ProgressDialog dialog =new ProgressDialog(MainActivity.this);
                dialog.setTitle("正在加载");
                dialog.setMessage("正在加载中,请稍后.....");
                dialog.show();
            }
        });
        // TODO 4、模拟下载过程
        btn_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ProgressDialog dialog = new ProgressDialog(MainActivity.this);
                dialog.setTitle("下载");
                // TODO 设置进度条的样式
                //转圈进度样式
//                dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                //百分比进度样式(0%--------100%)
                dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                dialog.show();

                // TODO 创建子线程用于更新当前进度
                new Thread(){
                    @Override
                    public void run() {
                        for (int i=1; i<=100; i++){
                            dialog.setProgress(i);
                            try {
                                Thread.sleep(100);
                            }catch (InterruptedException e){
                                throw new RuntimeException(e);
                            }
                        }
                        //关闭弹出框
                        dialog.dismiss();
                    }
                }.start();
            }
        });
        // TODO 5、自定义样式弹出框
        btn_5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //创建弹出框
                Dialog dialog=new Dialog(MainActivity.this);
                dialog.setTitle("登录");
                //设置自定义的布局页面mylogin.xml文件加载到对话框里
                dialog.setContentView(R.layout.mylogin);
                dialog.show();
            }
        });

    }
}

Running UI effect:

 

Guess you like

Origin blog.csdn.net/qq_53376718/article/details/130411654