【问题标题】:android edittext in a custom notification layout?android edittext 在自定义通知布局中?
【发布时间】:2016-06-09 05:25:28
【问题描述】:

想法是在自定义通知布局中显示电话号码,如果电话号码正常,则用户接受电话号码并使用该电话号码启动待处理的意图。否则,用户可以选择在同一通知中编辑电话号码,并在号码正确后接受。是否可以使用自定义通知布局来做到这一点?哪些 API 版本支持此类通知?

【问题讨论】:

  • 您不能在 RemoteViews 对象中使用`EditText`。此页面上列出了允许的视图集:developer.android.com/guide/topics/appwidgets/…
  • whatsapp 消息中的快速回复呢?
  • 这是以其他方式实现的。
  • 你知道是什么方式吗?
  • 你的谷歌搜索和我的一样好。

标签: android android-layout material-design android-notifications android-notification-bar


【解决方案1】:

@Karakuri 是对的。但Android宣布了新版本Android N。Android N支持内联回复。

添加内联操作。

  1. 创建一个可添加到通知操作的 RemoteInput.Builder 实例。此类的构造函数接受系统用作文本输入键的字符串。稍后,您的手持应用会使用该键来检索输入的文本。
// Key for the string that's delivered in the action's intent.
private static final String KEY_TEXT_REPLY = "key_text_reply";
String replyLabel = getResources().getString(R.string.reply_label);
RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY)
        .setLabel(replyLabel)
        .build();
  1. 使用 addRemoteInput() 将 RemoteInput 对象附加到操作。
// Create the reply action and add the remote input.
Notification.Action action =
        new Notification.Action.Builder(R.drawable.ic_reply_icon,
                getString(R.string.label), replyPendingIntent)
                .addRemoteInput(remoteInput)
                .build();
  1. 将操作应用于通知并发出通知。
// Build the notification and add the action.
Notification newMessageNotification =
        new Notification.Builder(mContext)
                .setSmallIcon(R.drawable.ic_message)
                .setContentTitle(getString(R.string.title))
                .setContentText(getString(R.string.content))
                .addAction(action))
                .build();

// Issue the notification.
NotificationManager notificationManager =
        NotificationManager.from(mContext);
notificationManager.notify(notificationId, newMessageNotification);

从内联回复中检索用户输入,访问官方文档: https://developer.android.com/preview/features/notification-updates.html

向后兼容性:

通知组和远程输入都是 自 Android 5.0(API 级别 21)起支持 Android 的通知 API 佩戴设备。如果您已经使用这些 API 构建了通知, 您必须采取的唯一措施是验证应用行为 对应于上述指南,并考虑 实现 setRemoteInputHistory()。

为了支持向后兼容,相同的 API 是 可与支持库的 NotificationCompat 类一起使用, 允许您构建适用于早期 Android 的通知 版本。在手持设备和平板电脑上,用户只能看到摘要 通知,因此应用程序仍应具有收件箱样式或 整个信息的等效通知代表 组的内容。由于 Android Wear 设备允许用户查看所有 即使在较旧的平台级别上,您也应该构建子通知 无论 API 级别如何,子通知。

【讨论】:

  • 谢谢@USKMobility,你知道android N之前版本的其他替代品吗?
  • 这个api还提供了backword兼容性。
  • 似乎没有向后兼容性。在 Anroid N 之前的设备中,单击操作按钮后不会出现文本框。谁能确认一下?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-13
  • 2017-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多