【问题标题】:Arrays and Spinners数组和微调器
【发布时间】:2014-12-07 16:19:15
【问题描述】:

我有一个非常具体的任务需要使用以下元素来完成。 我必须开发一个登录页面,在该页面中我必须创建一个我已经完成的下拉框(微调器)。 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]);
     }

  }

【问题讨论】:

  • Stack Overflow 用于编程问题。你有什么问题?
  • 我不知道如何让它工作。我无法让名称与密码匹配。

标签: java android arrays spinner


【解决方案1】:

你必须重写 setOnItemSelectedListener

spinner.setOnItemSelectedListener(新 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 获取有关微调器处理的示例。

【讨论】:

    猜你喜欢
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多