【发布时间】:2017-02-08 10:41:59
【问题描述】:
我想生成字母和数字随机密码,我有两个开关,一个用于字母,一个用于数字,当两个开关都打开时,同时生成字母和数字字符串,但当我的密码长度为 4 时时间它只以 alpha 显示,但我想要数字和 alpha。
我的代码如下所示
if (switchLetters.isChecked() && switchDigits.isChecked()) {
randomString(mProgress, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", txtPassword);
}
String randomString(int len, String mPassword, TextView mTextView) {
SecureRandom rnd = new SecureRandom();
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++)
sb.append(mPassword.charAt(rnd.nextInt(mPassword.length())));
mTextView.setText(sb.toString());
return sb.toString();
}
【问题讨论】:
标签: android random alphanumeric