gpt4 book ai didi

java - 无法生成固定大小的字母数字符号字符串

转载 作者:行者123 更新时间:2023-11-29 04:09:26 25 4
gpt4 key购买 nike

我正在构建一个应用程序,如果用户没有他/她自己的密码想法,它会为用户建议一个强密码。这个想法是用户选择密码的大小(4 个单选按钮)、密码特征(4 个复选框)和点击生成。基于这个选择,我正在尝试生成一个随机字符串:

private String generatedPassword(){
int length = 0;
StringBuilder generatedString = new StringBuilder();
Random rand = new Random();

if(radio4.isSelected()){length = 4;}
else if(radio8.isSelected()){length = 8;}
else if(radio12.isSelected()){length = 12;}
else if(radio16.isSelected()){length = 16;}

if(passwordABCD.isSelected()){totalCharacters += capitalLetters;}
if(passwordabcd.isSelected()){totalCharacters += lowercaseLetters;}
if(password0123.isSelected()){totalCharacters += numbers;}
if(passwordSymbols.isSelected()){totalCharacters += characters;}

char[] text = new char[length];

for(int i = 0; i < length; i++){text[i] = totalCharacters.charAt(rand.nextInt(length));}

for (char c : text) { generatedString.append(c); }

return generatedString.toString();
}

并在用户点击“生成”时设置该字符串的EditText

GenerateDialog(@NonNull Context context) {
super(context, android.R.style.Theme_NoTitleBar_Fullscreen);
setContentView(R.layout.dialog_layout);

...

generatePassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
passwordEditText.setText(generatedPassword());
}
});

...
}

但不幸的是,passwordEditText Edit Text 字段中看不到任何内容。

最佳答案

尝试在按钮上使用 isChecked() 方法而不是 isSelected()

此外,您可能希望通过删除不必要的 char[] 数组来简化代码,而是执行如下操作:

for (int i = 0; i < length; i++) {
generatedString.append(totalCharacters.charAt(rand.nextInt(totalCharacters.length()));
}

关于java - 无法生成固定大小的字母数字符号字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55948661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com