第一行代码:Android(第二版)——第三章笔记(二)

本文是《第一行代码:Android(第二版)》第三章笔记,主要介绍ProgressBar、AlertDialog、ProgressDialog、Notification和Toolbar等常用控件的使用方法。包括对话框的实现、进度条展示、通知管理及悬浮窗功能的代码演示和效果展示。
摘要由CSDN通过智能技术生成

参考书籍:第一行代码:Android(第二版)(郭霖):第三章

一、常用控件使用方法

6、ProgressBar

用于在界面上显示一个进度条,表明程序正在加载一些数据,运行之后会看到屏幕中有一个圆形的进度条正在旋转

<!--
 android:visibility:控制是否可见visible(可见),invisible(不可见),gone(不可见且不占用地方)
 style="@style/Widget.AppCompat.ProgressBar.Horizontal":设置水平进度条
android:max="100":进度条的最大值
android:progress:进度条已完成进度值
android:indeterminate:如果设置成true,则进度条不精确显示进度
style="?android:attr/progressBarStyleHorizontal"水平进度条
  -->
    <ProgressBar
        android:id="@+id/progress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        style="?android:attr/progressBarStyleHorizontal"
        android:max="100"/>


<!--触动按钮时可以停止转动,部分Java代码-->
progressBar=(ProgressBar)findViewById(R.id.progress);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()){
                    case R.id.button1:
                        String inputText=editText.getText().toString();
                        //消息提示
                        Toast.makeText(MainActivity.this,inputText,Toast.LENGTH_SHORT).show();
                       //通过按钮监控加载进度
//                        if(progressBar.getVisibility()==View.GONE){
//                            progressBar.setVisibility(View.VISIBLE);
//                        }else{
//                            progressBar.setVisibility(View.GONE);
//                        }
                        //动态修改水平进度条数据,每点击一次按钮进度条就加10
                        int projress=progressBar.getProgress();
                        projress=projress+10;
                        progressBar.setProgress(projress);
                        break;
                    default:
                        break;
                }
            }
});

7、AlertDialog

可以在当前的界面弹出一个对话框,这个对话框是置顶于所以界面元素之上的,能够屏蔽掉其他控件的交互能力,因此AlertDialog一般都是用于提示一些非常重要的内容或者警告信息,例如防止用户误删重要内容,在删除前弹出一个确认对话框

实现方式:
AlertDialog.Builder builder=new AlertDialog.Builder(context); 构建Dialog的各种参数
Builder.setIcon(int iconid); 添加ICON
Builder.setTitle(CharSequence title); 添加标题
Builder.setMessage(CharSequence message); 添加消息
Builder.setView(View view); 设置自定义布局
Builder.create();创建Dialog 创建Dialog
Builder.show(); 显示对话框
setPositiveButton 确定按钮
setNegativeButton 取消按钮
setNeutralButton 中间按钮
代码演示一:
//onCreate()方法里面添加此代码
   @Override
    protected void onCreate(Bundle savedInstanceState) {
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
        Button button=(Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
   
            @Override
            public void onClick(View view) {
   
/*
   首先通过AlertDialog.Builder创建一个AlertDialog的实例,然后可以为这个话题框设置标题、内容、
   可否使用back键关闭对话框等属性,接下来调用setPositiveButton()方法为对话框设置确定按钮的点击事件
  调用setNegativeButton()方法设置取消按钮的点击事件,最后调用show()方法将对话框显示出来
*/
                switch(view.getId()){
   
                    case R.id.button1:
                        AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
                        dialog.setTitle("This is a dialog");
                        dialog.setMessage("Something important.");
                        //设置是否可以用back键返回
                        dialog.setCancelable(false);
                        dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
   
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
   
                            }
                        });
                        dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
   
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
   
                            }
                        });
                        dialog.show();
                        break;
                    default:
                        break;
                }
            }
        });

    }
}

运行结果:

在这里插入图片描述

代码演示二:
布局文件一(主布局)activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">
    <Button
        android:id="@+id/bu1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示对话框"
        android:onClick="xyClick"/>
</LinearLayout>

布局文件二:
dialog_view.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:background="#ffff00">

    <ImageView

        android:src="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="哈哈哈,天气很好"/>
</LinearLayout>
package com.example.test;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

废材终结者

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

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

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

打赏作者

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

抵扣说明:

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

余额充值