如何动态隐藏或显示SWT密码

sta*_*tar 5 swt

在我的登录对话框中,有一个按钮:

Text pwdT = new Text(container, SWT.BORDER|SWT.PASSWORD);
Button plainBtn  = new Button(container,SWT.CHECK);
Run Code Online (Sandbox Code Playgroud)

如果我选择plainBtn,我希望显示的密码pwdT更改为纯文本而不是密文?有谁知道如何做到这一点?

Rüd*_*ann 6

setEchoChar()方法可用于控制是否显示输入的字符。

要显示实际输入的字符,请像这样清除 echo 字符:

text.setEchoChar('\0');
Run Code Online (Sandbox Code Playgroud)

您甚至可以创建Text不带SWT.PASSWORD样式标志的小部件,并在运行时仅更改密码字符。

如果您的某些目标平台不支持更改 echo 字符(如 macOS),您可以重新创建不带样式SWT.PASSWORD标志的密码文本字段。例如:

Text oldText = text;
Composite parent = oldText.getParent();
Control[] tabList = parent.getTabList();
// clone the old password text and dispose of it
text = new Text(parent, SWT.BORDER);
text.setText(oldText.getText());
text.setLayoutData(oldText.getLayoutData());
oldText.dispose();
// insert new password text at the right position in the tab order
for(int i = 0; i < tabList.length; i++) {
  if(tabList[i] == oldText) {
    tabList[i] = text;
  }
}
parent.setTabList(tabList);
parent.requestLayout();
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你!然而令人难以置信的是,我们必须重新创建该场才能改变其状态。 (3认同)