2014-12-07 32 views
0

我有一個非常具體的任務,需要使用下列元素來完成。 我必須開發一個登錄頁面,在那個頁面上我必須創建一個我已經完成的下拉框(微調框)。微調包含4個名字。但是,當從下拉框中選擇名稱時,用戶必須輸入密碼(4位數字)。這是棘手的地方,我需要幫助。我必須使用數組來連接來自微調控制器的名稱和與每個用戶相關的一組4個硬編碼密碼,例如, user1密碼是1234,否則密碼無效。 我已經問過關於這個特定主題的幫助,有些人提出了不同的方法,但我必須強調,我需要爲這個頁面使用並行數組。 我將包含一些我一直在嘗試使用的代碼。我的問題是如何將我的陣列連接到spinners,以便用戶選擇必須輸入他們特定的密碼? 目前我無法在輸入時將用戶連接到他們的特定密碼。陣列和紡紗器

 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]); 
    } 

    } 
+0

堆棧溢出用於編程問題。你的問題是什麼? – CommonsWare 2014-12-07 16:19:53

+0

問題是什麼? – 2014-12-07 16:40:52

+0

我不知道如何讓它工作。我無法獲得與密碼相匹配的名字。 – ExarchTemplar 2014-12-07 16:43:46

回答

0

你必須重寫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