数组和旋转器

时间:2014-12-07 16:19:15

标签: java android arrays spinner

我有一个非常具体的任务需要使用以下元素完成。 我必须开发一个登录页面,在那个页面我必须创建一个我已经完成的下拉框(微调框)。 Spinner包含4个名字。但是,当从下拉框中选择名称时,用户必须输入密码(4位数字)。这是它变得棘手的地方,我需要帮助。我必须使用数组来连接微调器中的名称和一组与每个用户相关的4个硬编码密码,例如: user1密码为1234,否则密码无效。 我已就此特定主题寻求帮助,有些人提出了不同的方法,但我必须强调,我需要为此页面使用并行数组。 我将包含一些我一直在尝试使用的代码。我的问题是如何将我的数组连接到微调器,以便选择的用户必须输入他们的特定密码? 目前,我无法在输入时将用户连接到他们的特定密码。

     my spinner contains four names// spinner=(Spinner) findViewById(R.id.names);
  ArrayAdapter adapter=ArrayAdapter.createFromResource(this, 
   R.array.names,android.R.layout.simple_spinner_item);
  spinner.setAdapter(adapter);
  spinner.setOnItemSelectedListener(this);

 my passcode login //public void onTextChanged(CharSequence s, int start, int before,
  int count)
   {
  if (passcodeEntered.getText().toString().length() == 4)
  {
     if ((passcodeEntered.getText().toString().equalsIgnoreCase("1234")))
     {
        Intent myIntent = new Intent(MainActivity.this, Summary.class);
        startActivity(myIntent);

     } else
     {
        Toast.makeText(getBaseContext(), "Invalid Passcode",          

        Toast.LENGTH_SHORT).show();

     finally here is some code I was trying to use in my arrays//

  public void passCodes(){

  String[] names = {"user1", "user2", "user3", "user4"};
  String [] passcodes = {"1234", "4321", "5678", "8765"};
  int passcode = keyboard.nextInt();

  for (int i = 0; i < names.length; i++)
  {



     if (passcodes[i]==passcode){
        System.out.println("Go to next page");
        System.out.println(names[i]);
     }

  }

1 个答案:

答案 0 :(得分:0)

您必须覆盖setOnItemSelectedListener

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
  //here check for the position, this will tell you which item in the dropdown was selected by the user. You can use this position as index to get the passcode from the array and match it with the passcode entered by user here and accordingly either call another activity or throw Toast message to user. 


}

});

有关微调器处理的示例,请参阅this blog