java使用Android创建强大的密码生成器
因此,我正在构建一个应用程序,根据用户的规格生成一个强大的密码。UI的规范部分看起来像this
以下是Main活动的onCreate。java类。我尝试创建一些逻辑,比如当选中某个单选按钮时if语句,以及每当选中复选框时向字符串变量添加允许的字符。视图对象都是全局的,但我不知道如何在用户设置的字符限制内,使用每个允许的字符中的至少一个创建随机字符串
以下是代码:
private static int MAX_LENGTH;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
capitalLetter = (CheckBox) findViewById(R.id.capital_letter);
lowercaseLetter = (CheckBox) findViewById(R.id.lowercase_letter);
numbers = (CheckBox) findViewById(R.id.numbers);
symbols = (CheckBox) findViewById(R.id.symbols);
passGroup = (RadioGroup) findViewById(R.id.passRadioGroup);
sizeFour = (RadioButton) findViewById(R.id.size_four);
sizeEight = (RadioButton) findViewById(R.id.size_eight);
sizeTwelve = (RadioButton) findViewById(R.id.size_twelve);
sizeSixteen = (RadioButton) findViewById(R.id.size_sixteen);
passHint = (EditText) findViewById(R.id.passwordHint);
passShow = (TextView) findViewById(R.id.passwordDisplay);
passGenerate = (Button) findViewById(R.id.passwordGenerate);
passClear = (Button) findViewById(R.id.passwordClear);
String allowedCharacters = "";
// Determines the types of characters permitted when a check box is checked.
if (capitalLetter.isChecked()) {allowedCharacters += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";}
if (lowercaseLetter.isChecked()) {allowedCharacters += "abcdefghijklmnopqrstuvwxyz";}
if (numbers.isChecked()) {allowedCharacters += "0123456789";}
if (symbols.isChecked()) {allowedCharacters += "!@#$%^&*()_-+=<>?/{}~|";}
//Determines the length of the string based on which radio button the user has selected.
int checkedRadioButtonId = passGroup.getCheckedRadioButtonId();
if (checkedRadioButtonId == 1) {MAX_LENGTH = 4;}
if (checkedRadioButtonId == 2) {MAX_LENGTH = 8;}
if (checkedRadioButtonId == 3) {MAX_LENGTH = 12;}
if (checkedRadioButtonId == 4) {MAX_LENGTH = 16;}
}
# 1 楼答案
关于生成随机字符串的基本思想:
# 2 楼答案
检查以下答案: