【发布时间】:2017-06-11 14:09:22
【问题描述】:
所以我正在构建一个应用程序,该应用程序可以根据用户的规范生成强密码。 UI 的规范部分看起来像 this。
以下是 MainActivity.java 类的 onCreate。我尝试创建一些逻辑,例如选中某个单选按钮时的 if 语句,以及在选中复选框时将允许的字符添加到 String 变量。 View 对象都是全局的 btw 但我无法弄清楚如何在用户设置的字符限制内使用每个允许的字符中的至少一个来创建随机字符串。
代码如下:
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;}
}
【问题讨论】:
-
使用
Random类 -
我之前没有实现过。