摸鱼学Android 十九(提示信息)

UI控件之九

1 Toast(消息提示框)

1.1 说明

Toast是一个View,用于快速为用户提供少量信息。
Toast没有按钮,也不会获得焦点,不影响用户输入,一段时间后自动消失。

1.2 使用

Toast 最常见的创建方式是使用静态方法 Toast.makeText()。
Toast.makeText()有3个参数:

  • 第一个参数:当前的上下文环境。可用getApplicationContext()或this,
  • 第二个参数:要显示的字符串。也可是R.string中字符串ID
  • 第三个参数:显示的时间长短。Toast默认的有两个LENGTH_LONG(长)和LENGTH_SHORT(短),也可以使用毫秒如2000ms

1.3 常用方法

  • setGravity:设置显示位置
  • setView:设置布局,用于自定义
  • setDuration:设置显示时间

1.4 实例 - 自定义Toast

  1. 定义Toast布局toast_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <ImageView
            android:id="@+id/toast_image"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/music"/>

    <TextView
            android:id="@+id/toast_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="你点击按钮弹出了消息~"
    />
</LinearLayout>
  1. 显示Toast方法
    public void showMyToast(View view) {
        Toast toast = new Toast(this);
        View toastView = LayoutInflater.from(this).inflate(R.layout.toast_view, null);
        toast.setView(toastView);
        toast.show();
    }
  1. 添加按钮绑定上面的方法
  2. 运行App,点击按钮
    在这里插入图片描述

2 Notification(状态通知栏)

2.1 说明

应用程序发出一条通知后,手机最上方的状态栏会显示一个通知的图标,下拉状态栏就可以看到通知的详细内容。
Notification有两种视觉风格,一种是标准视图(Normal View),另外一种是大视图(Big view)。标准视图在Android中各版本是通用的,但是对于大视图而言,仅支持Android4.1+的版本。
标准视图结构如下:在这里插入图片描述

  • 1: 通知标题
  • 2: 大图标
  • 3: 通知内容
  • 4: 通知消息
  • 5: 小图标
  • 6: 通知时间,一般为系统时间,也可以使用setWhen()设置。

相比之下,大视图多了一个7-详情区域:
在这里插入图片描述

2.2 创建方法

  1. 创建一个NotificationManager来对通知进行管理
//serviceName为系统服务名称
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  1. 使用Builder构造器来创建Notification对象
    //创建Notification,传入Context和channelId
    Notification notification = new NotificationCompat.Builder(this, name)
         .setAutoCancel(true)
         .setContentTitle(title)
         .setContentText(text)
         .setWhen(System.currentTimeMillis())
         .setSmallIcon(R.mipmap.ic_launcher)
         .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
         .setContentIntent(pendingIntent)
         //在build()方法之前还可以添加其他方法
         .build();
  1. 创建通知渠道
    NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
    //为NotificationManager设置通知渠道
    notificationManager.createNotificationChannel(channel);
  1. 调用NotificationManager的notify方法让通知显示
notificationManager.notify(noticeId, notification);
  1. 让通知从状态栏消失
//方法一,主动取消
notificationManager.cancel(noticeId);

//方法二,自动取消,创建时设置
setAutoCancel(true)

2.3 实例

  1. 定义通知方法
public class MainActivity extends AppCompatActivity {
    private NotificationManager manager;

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

        manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        createNotificationChannel("notice", "通知", importance);
    }

    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        NotificationManager notificationManager = (NotificationManager) getSystemService(
                NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);
    }

    public void showNotification(View view){
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        Notification notification = new NotificationCompat.Builder(this, "notice")
                .setAutoCancel(true)
                .setContentTitle("收到通知")
                .setContentText("你刚刚都干了什么")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.chat)
                .setColor(Color.parseColor("#F00606"))
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.chat))
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .build();
        manager.notify(1, notification);

    }
}
  1. 按钮添加点击事件
  2. 运行App,点击按钮
    在这里插入图片描述

3 AlertDialog(对话框)

3.1 说明

对话框置顶于所有元素之上,屏蔽其他控件的交互能力,因此一般用于提示重要信息。

3.2 使用

  1. 创建AlertDialog.Builder对象
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("提示:");
    builder.setMessage("这是一个普通对话框,");
    builder.setIcon(R.mipmap.ic_launcher);
    builder.setCancelable(true);
    //setPositive/Negative/NeutralButton()设置对应点击事件
    //可以通过setView设置自定义布局
  1. 创建AlertDialog对象
AlertDialog dialog = builder.create();
  1. 调用show()方法显示对话框
    dialog.show();

3.3 实例

  1. 创建显示dialog方法
public void showDialog(View view){
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("提示:")
                .setMessage("这是一个普通对话框")
                .setIcon(R.drawable.location)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).setCancelable(true);

        AlertDialog dialog = builder.create();

        dialog.show();
    }
  1. 按钮绑定点击事件
  2. 运行App,点击按钮
    在这里插入图片描述

4 其他对话框

常用的Dialog还有:

  • ProgressDialog(进度条对话框)
  • DatePickerDialog (日期选择对话框)
  • TimePickerDialog(时间选择对话框)

下面以ProgressDialog 为例进行说明

4.1 常用方法

ProgressDialog :

  • show:显示
  • setTitle:设置标题
  • setMessage:设置信息
  • setProgress:设置进度
  • setProgressStyle:设置进度条样式,水平或垂直
  • setIndeterminate:设置不确定模式
  • dismiss:关闭

4.2 使用

  1. 直接new ProgressDialog()
  2. 调用show方法

5 PopupWindow(悬浮框)

5.1 说明

PopupWindow与AlertDialog类似,都是在Activity最上层显示一个弹窗,不同的是,PopupWindow位置可以自定义。(AlertDialog需要通过WindowManager才能设置出现位置)

5.2 使用

  1. 构造PopupWindow,参数依次是加载的View,宽高,是否显示焦点
PopupWindow popWindow = new PopupWindow(view, width, height, true);
  1. 设置PopupWindow各个属性
	//设置加载动画
    popWindow.setAnimationStyle(R.anim.anim_pop);  
    //设置点击非PopupWindow区域,关闭悬浮框
    popWindow.setTouchable(true);
    popWindow.setTouchInterceptor(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
             // 这里如果返回true的话,touch事件将被拦截
            return false;
            }
    });
    //要为popWindow设置一个背景才有效
    popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    
    //设置popupWindow显示的位置,参数依次是参照View,x轴的偏移量,y轴的偏移量
    popWindow.showAsDropDown(v, 50, 0);
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值