文档管理中心
您当前浏览的HarmonyOS 5.0.0(API 12)文档归档不再维护,推荐您使用最新版本。详细请参考文档维护策略变更

强密码填充

本文导读

密码保险箱可以在用户需要输入一个新密码时,自动生成一个高强度的密码。用户选择使用生成的强密码时可以将这个密码填充到新密码输入框。

触发条件及注意事项

  • 已设置锁屏密码并且开启密码保险箱自动保存和填入账号和密码开关。
  • 界面中必须同时存在type为InputType.USER_NAME(表示用户名输入框)和InputType.NEW_PASSWORD(表示新密码输入框)的TextInput输入框组件。

    具体类型请参考输入框类型说明

  • TextInput组件的enableAutoFill属性的值为true(默认true)。
  • 用户在界面中首次点击新密码输入框时触发强密码弹窗,用户点击使用密码按钮可以将弹窗中显示的强密码自动填充到新密码输入框。
  • 开发者可以根据一定的规则和建议指定强密码生成规则。

注册

示例代码如下:

收起
自动换行
深色代码主题
复制
  1. @Entry
  2. @Component
  3. struct RegisterExample {
  4. pathInfos: NavPathStack = new NavPathStack();
  5. @State ReserveAccount: string = '';
  6. @State ReservePassword: string = '';
  7. @State enAbleAutoFill: boolean = true;
  8. onBackPress() {
  9. // 当非成功登录、返回等页面跳转时将enAbleAutoFill设置为false,密码保险箱不使能
  10. this.enAbleAutoFill = false;
  11. return false;
  12. }
  13. @Builder
  14. PageMap(name: string) {
  15. if (name === 'register_result_page') {
  16. RegisterResultPage()
  17. }
  18. }
  19. build() {
  20. Navigation(this.pathInfos) {
  21. Column() {
  22. Text("注册账号")
  23. .commonTitleStyles()
  24. TextInput({ placeholder: '用户名' })
  25. .commonInputStyles()
  26. .type(InputType.USER_NAME) // 账号框使用USER_NAME属性
  27. .onChange((value: string) => {
  28. this.ReserveAccount = value;
  29. })
  30. TextInput({ placeholder: '新密码' })
  31. .showPasswordIcon(true)
  32. .commonInputStyles()
  33. .type(InputType.NEW_PASSWORD) // 密码框使用 new Password 属性,可以触发生成强密码
  34. .enableAutoFill(this.enAbleAutoFill)
  35. .passwordRules('begin:[upper],special:[yes],len:[maxlen:32,minlen:12]')
  36. .onChange((value: string) => {
  37. this.ReservePassword = value;
  38. })
  39. Button('页面跳转')
  40. .commonButtonStyles()
  41. .enabled((this.ReserveAccount !== '') && (this.ReservePassword !== ''))
  42. .onClick(() => {
  43. this.pathInfos.pushPathByName('register_result_page', null)
  44. })
  45. Button('页面跳转(跳转前关闭autofill)')
  46. .commonButtonStyles()
  47. .enabled((this.ReserveAccount !== '') && (this.ReservePassword !== ''))
  48. .onClick(() => {
  49. this.enAbleAutoFill = false;
  50. this.pathInfos.pushPathByName('register_result_page', null)
  51. })
  52. }
  53. }
  54. .navDestination(this.PageMap)
  55. .height('100%')
  56. .width('100%')
  57. }
  58. }
  59. @Component
  60. struct RegisterResultPage {
  61. pathInfos: NavPathStack = new NavPathStack();
  62. build() {
  63. NavDestination() {
  64. Column() {
  65. Text("Result Page").commonTitleStyles()
  66. }.width('100%').height('100%')
  67. }.title("Result Page")
  68. .onReady((context: NavDestinationContext) => {
  69. this.pathInfos = context.pathStack;
  70. })
  71. }
  72. }
  73. @Extend(Text)
  74. function commonTitleStyles() {
  75. .fontSize(24)
  76. .fontColor('#000000')
  77. .fontWeight(FontWeight.Medium)
  78. .margin({ top: 24, bottom: 16 })
  79. }
  80. @Extend(TextInput)
  81. function commonInputStyles() {
  82. .placeholderColor(0x182431)
  83. .width('100%')
  84. .opacity(0.6)
  85. .placeholderFont({ size: 16, weight: FontWeight.Regular })
  86. .margin({ top: 16 })
  87. }
  88. @Extend(Button)
  89. function commonButtonStyles() {
  90. .width('100%')
  91. .height(40)
  92. .borderRadius(20)
  93. .margin({ top: 24 })
  94. }

修改密码

示例代码如下:

收起
自动换行
深色代码主题
复制
  1. @Component
  2. struct RegisterExample {
  3. pathInfos: NavPathStack = new NavPathStack();
  4. @State ReserveAccount: string = '';
  5. @State ReservePassword: string = '';
  6. @State enAbleAutoFill: boolean = true;
  7. onBackPress() {
  8. // 当非成功登录、返回等页面跳转时将enAbleAutoFill设置为false,密码保险箱不使能
  9. this.enAbleAutoFill = false;
  10. return false;
  11. }
  12. @Builder
  13. PageMap(name: string) {
  14. if (name === 'register_result_page') {
  15. RegisterResultPage()
  16. }
  17. }
  18. build() {
  19. Navigation(this.pathInfos) {
  20. Column() {
  21. Text("修改密码")
  22. .commonTitleStyles()
  23. TextInput({ placeholder: '用户名' })
  24. .commonInputStyles()
  25. .type(InputType.USER_NAME) // 账号框使用USER_NAME属性
  26. .onChange((value: string) => {
  27. this.ReserveAccount = value;
  28. })
  29. TextInput({ placeholder: '密码' })
  30. .showPasswordIcon(true)
  31. .commonInputStyles()
  32. .type(InputType.Password)
  33. .onChange((value: string) => {
  34. this.ReservePassword = value;
  35. })
  36. TextInput({ placeholder: '新密码' })
  37. .showPasswordIcon(true)
  38. .commonInputStyles()
  39. .type(InputType.NEW_PASSWORD) // 密码框使用 new Password 属性,可以触发生成强密码
  40. .enableAutoFill(this.enAbleAutoFill)
  41. .passwordRules('begin:[upper],special:[yes],len:[maxlen:32,minlen:12]')
  42. .onChange((value: string) => {
  43. this.ReservePassword = value;
  44. })
  45. Button('页面跳转')
  46. .commonButtonStyles()
  47. .enabled((this.ReserveAccount !== '') && (this.ReservePassword !== ''))
  48. .onClick(() => {
  49. this.pathInfos.pushPathByName('register_result_page', null)
  50. })
  51. Button('页面跳转(跳转前关闭autofill)')
  52. .commonButtonStyles()
  53. .enabled((this.ReserveAccount !== '') && (this.ReservePassword !== ''))
  54. .onClick(() => {
  55. this.enAbleAutoFill = false;
  56. this.pathInfos.pushPathByName('register_result_page', null)
  57. })
  58. }
  59. }
  60. .navDestination(this.PageMap)
  61. .height('100%')
  62. .width('100%')
  63. }
  64. }
  65. @Component
  66. struct RegisterResultPage {
  67. pathInfos: NavPathStack = new NavPathStack();
  68. build() {
  69. NavDestination() {
  70. Column() {
  71. Text("Result Page").commonTitleStyles()
  72. }.width('100%').height('100%')
  73. }.title("Result Page")
  74. .onReady((context: NavDestinationContext) => {
  75. this.pathInfos = context.pathStack;
  76. })
  77. }
  78. }
  79. @Extend(Text)
  80. function commonTitleStyles() {
  81. .fontSize(24)
  82. .fontColor('#000000')
  83. .fontWeight(FontWeight.Medium)
  84. .margin({ top: 24, bottom: 16 })
  85. }
  86. @Extend(TextInput)
  87. function commonInputStyles() {
  88. .placeholderColor(0x182431)
  89. .width('100%')
  90. .opacity(0.6)
  91. .placeholderFont({ size: 16, weight: FontWeight.Regular })
  92. .margin({ top: 16 })
  93. }
  94. @Extend(Button)
  95. function commonButtonStyles() {
  96. .width('100%')
  97. .height(40)
  98. .borderRadius(20)
  99. .margin({ top: 24 })
  100. }
在 指南 中进行搜索
请输入您想要搜索的关键词