是否可以创建一个可点击的类似Toast的通知?

2023-11-29

我需要显示一个最小侵入性的非阻塞通知,它是not与其显示的活动相关联(例如Toast) and这是可点击的。有谁知道这是否可能?不幸的是,看来Toast通知(自定义或其他)不可点击(即设置单击监听器对其观点没有影响)。我所知道的所有替代方案(即警报对话框, 弹出窗口 and Crouton)似乎显示了一个与它所显示的活动相关的通知(即,当活动完成时,它们不会继续显示)。有什么建议么?


您可以使用PopupWindow,添加一个onClickListener并添加一个handlern 次后自动取消(就像 a 的行为一样)toast)。像这样的东西:

public static void showToast(Activity a, String title, String message) {

    // inflate your xml layout
    LayoutInflater inflater = a.getLayoutInflater();
    View layout = inflater.inflate(R.layout.custom_toast,
            (ViewGroup) a.findViewById(R.id.toast_layout_root));

    // set the custom display
    ((TextView) layout.findViewById(R.id.title)).setText(title);
    ((TextView) layout.findViewById(R.id.message)).setText(message);

    // initialize your popupWindow and use your custom layout as the view
    final PopupWindow pw = new PopupWindow(layout,
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT, true);

    // set windowType to TYPE_TOAST (requires API 23 above)
    // this will make popupWindow still appear even the activity was closed
    pw.setWindowLayoutType(WindowManager.LayoutParams.TYPE_TOAST);
    pw.showAtLocation(layout, Gravity.CENTER | Gravity.TOP, 0, 500);

    // handle popupWindow click event
    layout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // do anything when popupWindow was clicked
            pw.dismiss(); // dismiss the window
        }
    });

    // dismiss the popup window after 3sec
    new Handler().postDelayed(new Runnable() {
        public void run() {
            pw.dismiss();
        }
    }, 3000);
}

xml布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/toast_layout_root"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#000"
              android:orientation="vertical"
              android:elevation="10dp"
              android:padding="20dp">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#FFF"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#FFF"/>

</LinearLayout>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

是否可以创建一个可点击的类似Toast的通知? 的相关文章

  • 在 Android 上通话结束时启动活动

    我想在通话结束时启动一项活动 找不到任何对此的参考 我该怎么做 我还没有尝试过这个 但我假设你可以运行一些服务 始终在后台运行 它利用电话状态监听器 http developer android com reference android
  • 设置文本视图 Android 的文本颜色

    在 string xml 文件中我使用以下标签
  • 检查 WebView 元素时的 UiAutomator 错误

    I have recently started automation testing in android and was using UiAutomator tool for inspecting UI elements Surprisi
  • android edittext中的字符映射

    我想让我的编辑文本就像我写字符 g 时一样 它是相关的映射自定义字符应该写成印地语中的 我认为应该有字符映射 但没有知识任何人都可以帮助我 怎么做 其他应用程序https play google com store apps details
  • Cheesesquare:enterAlways 会产生错误的布局

    Adding enterAlways到 Cheesesquare 演示的滚动标志
  • AdapterContextMenuInfo 始终为 null

    我尝试通过 android 开发文档中的书来做到这一点 this didn t create a menu i don t know why registerForContextMenu getListView setListAdapter
  • 如何正确释放Android MediaPlayer

    我正在尝试向我的 Android 应用程序添加一个按钮 当点击该按钮时它会播放 MP3 我已经让它工作了 但没有办法释放 mediaPlayer 对象 因此即使在我离开活动后它仍然会继续播放 如果我在react 方法之外初始化MediaPl
  • 如何在 Linux 内核中定义并触发我自己的新软中断?

    我想在 Linux 内核中创建自己的软中断 这是正确的方法吗 In the init我想触发该模块的softirq我将添加一个调用 394 void open softirq int nr void action struct softir
  • Dialog.setTitle 不显示标题

    我正在尝试向我的对话框添加自定义标题 但是每当我运行我的应用程序时 它都不会显示标题 我创建对话框的代码是 final Dialog passwordDialog new Dialog this passwordDialog setCont
  • 以编程方式将文本颜色设置为主要 Android 文本视图

    如何设置我的文本颜色TextView to android textColorPrimary以编程方式 我已经尝试了下面的代码 但它将 textColorPrimary 和 textColorPrimary Inverse 的文本颜色始终设
  • 在 android 中建立与 MySQL 的池连接

    我需要从我的 Android 应用程序访问 MySQL 数据库 现在所有的工作都通过 DriverManager getConnection url 等等 但我必须从多个线程访问数据库 所以我必须使用连接池 问题1 是 com mysql
  • okhttp 获取失败响应

    我已经在我的 android 客户端中实现了 okhttp 来进行网络调用 当我收到失败响应时 我会收到失败代码以及与该代码相关的文本作为消息 但我没有收到服务器发送给我的自定义失败响应 在我实施的代码中的失败响应中 我收到的消息只是 错误
  • Mipmap 与可绘制文件夹[重复]

    这个问题在这里已经有答案了 我正在使用 Android Studio 1 1 Preview 1 我注意到 当我创建一个新项目时 我得到以下层次结构 不同 DPI 的 Mipmap 文件夹 不再有不同 DPI 的可绘制文件夹 我应该将所有资
  • OnLongClickListener 不工作

    我有一个ImageView 我需要使用onLongClickListener对于图像视图 当我使用这段代码时 什么也没有发生 Code gallery Gallery findViewById R id gall1 gallery setA
  • 如何在 Android 中从 WorkManager 取消工作?

    我已经保存了 WorkManagerUUID转换成String在领域数据库中 这是代码 Constraints constraints new Constraints Builder setRequiredNetworkType Netwo
  • Android 如何将总天数准确更改为年、月、日?

    我正在做一个应用程序 该应用程序与根据给定的生日日期输入获取一个人的年龄有关 为此 我从下面的代码中获取从该日期到当前日期的总天数 String strThatDay 1991 05 10 SimpleDateFormat formatte
  • 卡片视图 单击卡片移至新活动

    我是 Android 编程新手 正在研究卡片布局 我想知道如何使其可点击 android clickable true android foreground android attr selectableItemBackground 我的卡
  • 通过电子邮件发送文本文件附件

    我正在尝试附加一个文本文件以便通过电子邮件发送 但每当我打开电子邮件应用程序时 它都会说该文件不存在 请帮助 Intent i new Intent Intent ACTION SEND i setType text plain i put
  • Dagger 2 中“HasFragmentInjector”的实际用法是什么

    我之前已经实现了 dagger2 v2 2 但现在他们也添加了 dagger android 部分 所以我正在用它创建示例项目 我知道旧的方法论 Provide and Modules and 成分等注释 但从 Dagger 2 8 开始
  • 发布的 Android apk 出现错误“包文件未正确签名”

    我最近将我的应用程序上传到 Android 市场 但是由于错误 下载时它拒绝运行 包文件未正确签名 我首先使用 eclipse 发布了数据包 右键单击导出 创建密钥库然后发布 但它拒绝工作 然后我下载了 keytool 和 jarsigne

随机推荐