AWT/SWT 中的 iPhone 密码字段?

发布于 2024-10-04 12:41:04 字数 356 浏览 7 评论 0原文

我想为我的 eclipse 产品创建一个特殊的密码对话框,它与屏幕键盘一起使用。

如果我可以使用像 iPhone 密码字段这样的组件,那就太好了。在此字段中,添加的字符会显示一秒钟,然后会转换为“*”字符以隐藏完整的密码。

是否存在 jar/库,这是在 AWT 或 SWT 中实现的吗?

编辑:

我可以尝试从头开始实现它(SWT),但对于这些,我必须为密码文本组件创建一个非常特殊且复杂的 KeyListener。我必须捕获 keyReleased 事件并将字符手动设置到字段中。

到目前为止,我无法在网络上找到任何库。 也欢迎提出如何实施的建议

I want to create a special Password Dialog for my eclipse product, which is used with an on screen keyboard.

It would be very nice, if i could use a component like the IPhone Password field. In this field, the added character is shown for a second and after the second it is converted into the '*' character for hiding the complete password.

Did a jar/library exists, this is implemented in AWT or SWT?

Edit:

I could trying to implement it from scratch (SWT), but for these i would have to create a very special and complicated KeyListener for the password Text component. I would have to catch the keyReleased event and set the characters manually into the field.

So far i was not able to find any libraries in the web. Suggestion how this can be implemented are welcome too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悟红尘 2024-10-11 12:41:04

这并不是一个完整的答案,而不是一个讨论的开始,我不知道有任何开箱即用的小部件可以做到这一点。

我的第一个想法是继承 swt Text 小部件并覆盖 setEchoChar 等人,但是在查看代码之后,这似乎并不可行,因为这个方法只是一个包装器:

OS.SendMessage (handle, OS.EM_SETPASSWORDCHAR, echo, 0);

如果有人知道操作系统特定的低级实施,这可能会有所帮助。

无论如何,采取不同的方法。我会避免使用 KeyListener 并在 Text-Widget 上使用 ModifyListener。

void addModifyListener(ModifyListener listener)

然后,您可以构建一个包装器,使用此侦听器捕获输入的文本,将其附加到本地保存的字符串/字符串缓冲区(或例如 Eclipse Preferencestore),并使用 setText(String s) 将修改后的全文发送到文本小部件,替换所有文本除最后一个回显字符(例如 *)之外的字符。

myText.setText((s.substring(0, s.length()-1)).replaceAll("[\\s\\S]","*")+s.charAt(s.length()-1));

这有点拼凑,但应该可行。

不那么简单的一点是 1 秒计时,但不会影响整个视图......

This is not really a full answer, rather than a discussion starter and I don't know of any out-of-the-box widgets which can do that.

My first idea was to inheriting the swt Text widget and overriding setEchoChar et al., but after looking at the code this doesn't really seem feasible, because this method is merely a wrapper around:

OS.SendMessage (handle, OS.EM_SETPASSWORDCHAR, echo, 0);

If anyone would know the OS specific low-level implementation, that might be helpful.

Anyway, on to a different approach. I would avoid the KeyListener and use a ModifyListener on the Text-Widget.

void addModifyListener(ModifyListener listener)

You could then build a wrapper which catches the entered text using this listener, appends it to a locally held string/stringbuffer (or e.g. the Eclipse Preferencestore) and send a modified full text to the Text widget using setText(String s), replacing all characters except the last by an echo character (e.g. *).

myText.setText((s.substring(0, s.length()-1)).replaceAll("[\\s\\S]","*")+s.charAt(s.length()-1));

This is a bit of a kludge, but it should work.

The not so straightforward bit is the 1 second timing, without stalling the whole view...

何止钟意 2024-10-11 12:41:04

根据 Jules 的说法,以下代码可以正常工作。
代码又快又快,我希望有一个更线程安全的解决方案。

originalString = new StringBuffer();

passwordField.addModifyListener(new ModifyListener() {

public void modifyText(ModifyEvent e) {

    synchronized (passwordField) {

         String s = passwordField.getText();

         String newS = s.replaceAll("[\\s\\S]", "*");
         if (newS.equals(s)) {
             while (originalString.length() > s.length()) {
                 originalString = originalString.deleteCharAt(originalString.length() - 1);
             }
             usernameField.setText(originalString.toString());
             return;
         }

        if (originalString.length() < s.length()) {
            originalString.append(s.charAt(s.length() - 1));
        }

        try {
            Thread.sleep(500);
        } catch (InterruptedException e1) {
        }
        passwordField.setText(newS);
    }

    passwordField.redraw();

    passwordField.setSelection(passwordField.getText().length());
    }

});

关键事件会被缓存,因此您可以添加更多字符,即使在线程等待时也是如此。
另一个问题是游标处理。当您设置文本时,光标始终移动到第一个位置。

我认为当它起作用时,它非常接近 iPhone 的解决方案。

Depending on what Jules said the following code is some kind of working.
The code is quick and fast and i would like to have a more thread safe solution.

originalString = new StringBuffer();

passwordField.addModifyListener(new ModifyListener() {

public void modifyText(ModifyEvent e) {

    synchronized (passwordField) {

         String s = passwordField.getText();

         String newS = s.replaceAll("[\\s\\S]", "*");
         if (newS.equals(s)) {
             while (originalString.length() > s.length()) {
                 originalString = originalString.deleteCharAt(originalString.length() - 1);
             }
             usernameField.setText(originalString.toString());
             return;
         }

        if (originalString.length() < s.length()) {
            originalString.append(s.charAt(s.length() - 1));
        }

        try {
            Thread.sleep(500);
        } catch (InterruptedException e1) {
        }
        passwordField.setText(newS);
    }

    passwordField.redraw();

    passwordField.setSelection(passwordField.getText().length());
    }

});

Key Events are cached, so you can add more characters, also when the Thread is waiting.
Another Problem is the Cursor handling. the Cursor always moves to the first position, when you set the Text.

I think when this is working it is very near to the iphone solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文