toast提示和alert提示_Android提供两个常用的消息弹出框【Toast和Alert】

Android提供两个常用的消息弹出框,Toast和Alert。

Toast

Toast是一种短暂的提示框,并不需要用户交互,也不会将focus移过来,因此可以适合大多数的场景,向用户进行信息提示。在之前的学习中,已经多次使用到Toast了。

创建一个Toast很简单,使用静态方法makeText(Context context, CharSequence text | int resId, int duration),将String(或者String的ID),以及显示的时间长短(LENGTH_SHORT或者LENGTH_LONG)就可以得到一个Toast的对象。

上面是最常用的方式,但是如果要显示成其他的view,而不仅仅是text,可以使用Toast的构造函数Toast(Context context),然后使用setView()设置所显示的view,通过setDuration()设置显示时间即可。

如果我们需要显示Toast,只要调用show()的方法即可。例如:

private void insertEmployee()

{

//调用显示Toast对话框

Toast.makeText(TestMsg.this, "haha,Insert Employee Successfully!", Toast.LENGTH_LONG).show();

};

Alert

传统的方式,是dialog box的形式,需采用AlertDialog,当一个警告框弹出之后,将获得focus,必须由用户来关闭它,用于显示重要的错误或者事件,必须确保用户知晓的场景或者一些验证的信息。

最容易的警告框通过AlertDialog.Builder类来创建,可以依次调用setMessage()设置显示文本内容,setTitle(),setIcon(),以及设置下面的按钮setPositiveButton(),setNeutralButton(),setNegativeButton(),

这几个button具体和名字没有什么关联,只是表明依次的位置随左中右,并且最多显示三个按键,我们需要设置这些按键的显示内容和点击触发的处理。最后调用show()方法,将其显示。

如果我们需要先创建builder对象,然后进行配置,最后再显示,可以先使用create(),然后设置上述内容,最后调用show()。一旦show()调用,则一直等等用户的处理。

下面是一个警告框的显示例子,我们对告警框的内容依次设置:

private void AlertDialog()

{

//Alert Dialog

new AlertDialog.Builder(TestMsg.this)

.setTitle("Alerting Message")

.setMessage("Ha Ha!")

.setNegativeButton("Close", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

//do nothing - it will close on its own

}

})

.show();

};

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先需要了解一下 Android 中通知的相关知识。Android 中的通知是通过 NotificationManager 来管理的,通知的显示效果是由 Notification 类的实例来控制的。一般情况下,我们可以使用 NotificationCompat 类来构造通知,可以兼容不同版本的 Android 系统。 接下来,我们来介绍一下如何通过自定义 Toast 实现悬浮通知效果: 1. 首先,在 AndroidManifest.xml 文件中添加权限声明: ```xml <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> ``` 2. 在代码中创建自定义的 Toast 类,并重写其 onWindowFocusChanged() 方法,用于创建悬浮通知: ```java public class FloatingToast extends Toast { private WindowManager mWindowManager; private View mView; private WindowManager.LayoutParams mParams; public FloatingToast(Context context) { super(context); mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); mView = LayoutInflater.from(context).inflate(R.layout.floating_toast, null); mParams = new WindowManager.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); mParams.gravity = Gravity.TOP | Gravity.START; } @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus) { mWindowManager.addView(mView, mParams); } else { mWindowManager.removeView(mView); } } } ``` 3. 在布局文件 floating_toast.xml 中定义悬浮通知的样式: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/floating_toast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_floating_toast" android:orientation="horizontal"> <ImageView android:id="@+id/iv_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_notification" /> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是一条悬浮通知" /> </LinearLayout> ``` 4. 在 Activity 中使用自定义 Toast 实现悬浮通知: ```java FloatingToast toast = new FloatingToast(this); toast.setDuration(Toast.LENGTH_LONG); toast.setView(LayoutInflater.from(this).inflate(R.layout.floating_toast, null)); toast.show(); ``` 5. 最后,记得在 Activity 的 onDestroy() 方法中销毁自定义 Toast 对象: ```java @Override protected void onDestroy() { super.onDestroy(); if (toast != null) { toast.cancel(); } } ``` 上述代码中的布局文件和相关资源文件可以根据需要自行修改,以实现不同的悬浮通知样式。同时,需要注意的是,由于 Android 8.0 及以上版本对通知权限进行了限制,如果需要在这些系统版本上显示悬浮通知,需要申请权限并设置 targetSdkVersion 为 25 或以下。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值