AlertDialog(对话框)

继承自Popup

对话框是一个原生对话弹出框,显示一个消息与最多三个按钮。关闭时会被自动释放。

使用“const {AlertDialog} = require('tabris');”引入该类。

属性

buttons

Type: {ok?: string, cancel?: string, neutral?: string}

含有按钮文本内容的对象。分别是这三种按钮:ok, cancelneutral。没有设置文本的按钮不会显示。示例: {ok:'Yes',cancel:'No'}显示‘Yes’和‘No’按钮,不显示‘neutral’按钮。

message

Type: string

对话框中显示的文本。

title

Type: string

对话框的标题。

事件

buttonsChanged

buttons属性改变时触发。

事件参数

  • target: this 事件触发的控件。

  • value: {ok?: string, cancel?: string, neutral?: string}buttons属性被设置的新的值。

close

当对话框被关闭时触发。

事件参数

  • target: this 事件触发的控件。

  • button: ‘ok’|’cancel’|’neutral’|’’ 关闭对话框的操作按钮类型。也可能是个空串,比如点了应用返回按钮而被关闭时。

closeCancel

当通过点击‘cancel’按钮关闭对话框时触发。

closeNeutral

当通过点击‘neutral’按钮关闭对话框时触发。

closeOk

当通过点击‘ok’按钮关闭对话框时触发。

messageChanged

message属性发生改变时触发。

事件参数

  • target: this 事件触发的控件。

  • value: stringmessage属性被设置的新的值。

titleChanged

title属性发生改变时触发。

事件参数

  • target: this 事件触发的控件。

  • value: stringtitle属性被设置的新的值。

示例

  1. const {AlertDialog, Button, ui} = require('tabris');
  2. // AlertDialog example
  3. new Button({
  4. left: 16, top: 'prev() 16', right: 16,
  5. text: 'Show simple dialog'
  6. }).on('select', () => {
  7. new AlertDialog({
  8. message: 'Your comment has been saved.',
  9. buttons: {ok: 'Acknowledge'}
  10. }).open();
  11. }).appendTo(ui.contentView);
  12. new Button({
  13. left: 16, top: 'prev() 16', right: 16,
  14. text: 'Show full featured dialog'
  15. }).on('select', () => {
  16. new AlertDialog({
  17. title: 'Conflict while saving',
  18. message: 'How do you want to resolve the conflict?',
  19. buttons: {
  20. ok: 'Replace',
  21. cancel: 'Discard',
  22. neutral: 'Keep editing'
  23. }
  24. }).on({
  25. closeOk: () => console.log('Replace'),
  26. closeNeutral: () => console.log('Keep editing'),
  27. closeCancel: () => console.log('Discard'),
  28. close: ({button}) => console.log('Dialog closed: ' + button)
  29. }).open();
  30. }).appendTo(ui.contentView);
  31. new Button({
  32. left: 16, top: 'prev() 16', right: 16,
  33. text: 'Show self closing dialog'
  34. }).on('select', () => {
  35. let alertDialog = new AlertDialog({
  36. message: 'This dialogs closes in 3 seconds.',
  37. buttons: {ok: 'OK'}
  38. }).open();
  39. setTimeout(() => alertDialog.close(), 3000);
  40. }).appendTo(ui.contentView);

还可参阅

原文:

https://youjingyu.github.io/Tabris-Documention/?folderName=api&pageName=AlertDialog.html