我正在尝试创建一个登录表单,您可以在其中从微调器中选择一个项目,然后需要在文本字段中输入正确的代码才能继续操作。这就是我得到的。
Spinner schoolList = findViewById(R.id.schoolList);
ArrayAdapter<String> schoolListAdapter = new ArrayAdapter<String>(CreateAccount.this, android.R.layout.simple_spinner_dropdown_item, schools);
schoolList.setAdapter(schoolListAdapter);
private String[] schools = new String[]{
"School 1",
"School 2,
"School 3"
};
private String[] schoolCodeArray = new String[] {
"password1",
"password2",
"password3"
};
我知道您可以检查School 1 = password1等等,但是此列表将非常长,因此我正在寻找更好的方法。诸如获取所选学校的索引并检查该索引是否等于schoolCodeArray中的索引并与文本字段中的内容匹配的东西。希望您能理解我的需求。我感谢所有帮助!
答案 0 :(得分:1)
如果您应该有两个微调器,一个用于学校,另一个用于密码,那么考虑项目顺序也可能有所不同。使您的逻辑灵活。
为此,请创建一个HashMap
,其中将学校作为密钥,并将密码作为值。
HashMap<String, String> credentialMap = new HashMap<String, String>();
credentialMap.put("School 1","Password 1")
credentialMap.put("School 2","Password 2")
一旦用户从两个微调器中选择了一个项目,然后需要执行一些按钮操作来验证它。为此
loginButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if(credentialMap.containsKey(schoolSpinnerItem)){
if(credentialMap.get(schoolSpinnerItem).equals(passwordSpinnerItem)){
// Login success
}
else{
// Login failed
}
}
}
});
答案 1 :(得分:0)
我想您已通过EditText
()初始化了findViewById
,因此请尝试将其放入onItemSelected
中:
String[] schools = new String[]{
"School 1",
"School 2",
"School 3"
};
final String[] schoolCodeArray = new String[] {
"password1",
"password2",
"password3"
};
Spinner schoolList = findViewById(R.id.schoolList);
ArrayAdapter<String> schoolListAdapter = new ArrayAdapter<String>(CreateAccount.this, android.R.layout.simple_spinner_dropdown_item, schools);
schoolList.setAdapter(schoolListAdapter);
schoolList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (schoolCodeArray[position].equals(editText.getText().toString().trim())) {
// your code here
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});