2011-11-18 24 views
0

我有四個輪子與數字0-9旋:Android的四個陣列

enter image description here

我希望能夠輸入文字到每個車輪從四個不同的陣列,所以每個車輪將有它自己特定的一組單詞。

該代碼張貼在下面,如果有人可以看一看並編輯代碼來糾正它,會非常有幫助。

public class PasswActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.passw_layout); 
     initWheel(R.id.passw_1); 
     initWheel(R.id.passw_2); 
     initWheel(R.id.passw_3); 
     initWheel(R.id.passw_4); 

     Button mix = (Button)findViewById(R.id.btn_mix); 
     mix.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       mixWheel(R.id.passw_1); 
       mixWheel(R.id.passw_2); 
       mixWheel(R.id.passw_3); 
       mixWheel(R.id.passw_4); 
      } 
     }); 
    } 

    // Wheel scrolled flag 
    private boolean wheelScrolled = false; 

    // Wheel scrolled listener 
    OnWheelScrollListener scrolledListener = new OnWheelScrollListener() { 
     public void onScrollingStarted(WheelView wheel) { 
      wheelScrolled = true; 
     } 
     public void onScrollingFinished(WheelView wheel) { 
      wheelScrolled = false; 
     } 
    }; 

    // Wheel changed listener 
    private OnWheelChangedListener changedListener = new OnWheelChangedListener() { 
     public void onChanged(WheelView wheel, int oldValue, int newValue) { 
      if (!wheelScrolled) { 

      } 
     } 
    }; 


    /** 
     * Initializes wheel 
     * @param id the wheel widget Id 
    */ 
    private void initWheel(int id) { 
      WheelView wheel = getWheel(id); 
      wheel.setViewAdapter(new NumericWheelAdapter(this, 0, 9)); 
      wheel.setCurrentItem((int)(Math.random() * 10)); 

      wheel.addChangingListener(changedListener); 
      wheel.addScrollingListener(scrolledListener); 
      wheel.setCyclic(true); 
      wheel.setInterpolator(new AnticipateOvershootInterpolator()); 
    } 

    /** 
    * Returns wheel by Id 
    * @param id the wheel Id 
    * @return the wheel with passed Id 
    */ 
    private WheelView getWheel(int id) { 
     return (WheelView) findViewById(id); 
    } 

    /** 
    * Tests entered PIN 
     * @param v1 
     * @param v2 
     * @param v3 
     * @param v4 
     * @return true 
    */ 
    private boolean testPin(int v1, int v2, int v3, int v4) { 
     return testWheelValue(R.id.passw_1, v1) && testWheelValue(R.id.passw_2, v2) && 
     testWheelValue(R.id.passw_3, v3) && testWheelValue(R.id.passw_4, v4); 
    } 

    /** 
    * Tests wheel value 
    * @param id the wheel Id 
    * @param value the value to test 
    * @return true if wheel value is equal to passed value 
    */ 
    private boolean testWheelValue(int id, int value) { 
      return getWheel(id).getCurrentItem() == value; 
    } 

    /** 
    * Mixes wheel 
    * @param id the wheel id 
    */ 
    private void mixWheel(int id) { 
      WheelView wheel = getWheel(id); 
      wheel.scroll(-25 + (int)(Math.random() * 50), 2000); 
    } 
} 
+0

看看你NumericWheelAdapter類,看看它用來構造適配器 – 2011-11-18 10:53:59

+0

你可能會張貼在這裏爲好,這樣我們可以幫助您根據修改您的需要 – 2011-11-18 11:04:13

+0

對不起,我很新,可以告訴我你的意思到底是什麼,你需要我發佈什麼? – Matt

回答

1

在Android中選取器控件從http://code.google.com/p/android-wheel/我已付出使用http://android-wheel.googlecode.com/svn/trunk/

然後一個項目我在Eclipse IDE中從源代碼導入了一個新的Android項目 - WheelDemo。 之後,我改變了: 在WheelDemo.java

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    //Since another activity is being called in this way, 
    //I am sending a list with every intent, but this can be changed 

    Map map = (Map) l.getItemAtPosition(position); 

    ArrayList<String> list = new ArrayList<String>(); 
    list.add("One"); 
    list.add("Two"); 

    Intent intent = (Intent) map.get("intent"); 
    intent.putStringArrayListExtra("list", list); 
    startActivity(intent); 
} 

CitiesActivity.java

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.cities_layout); 
     String[] arrayString = null; 

     ArrayList<String> list = new ArrayList<String>(); 
     list = getIntent().getExtras().getStringArrayList("list"); 

     if (list != null) { 
      arrayString = new String[list.size()]; 
      if (!list.isEmpty()) { 
       for (int i = 0; i < list.size(); i++) 
        arrayString[i] = list.get(i); 
      } else 
       System.out.println("list is empty"); 
     } else 
      System.out.println("list is null"); 

     final WheelView country = (WheelView) findViewById(R.id.country); 
     country.setVisibleItems(3); 
     country.setViewAdapter(new CountryAdapter(this)); 

     final String cities[][] = new String[][] { 
       new String[] { "New York", "Washington", "Chicago", "Atlanta", 
         "Orlando" }, 
       new String[] { "Ottawa", "Vancouver", "Toronto", "Windsor", 
         "Montreal" }, 
       new String[] { "Kiev", "Dnipro", "Lviv", "Kharkiv" }, 
       arrayString, }; 

..... 

然後我得到這樣一個畫面:

Wheel Widget Android

PS我已經使用WheelDemo源代碼,而不是一個

0

在源(kankan.wheel.widget),你必須也ArrayWheelAdapter類,用它來代替OD NumericWheelAdapter calass

+0

我已經嘗試將其更改爲新的ArrayWheelAdapter (null,new String [] {「Abc」,「Foo」,「Bar」}}這給了我沒有錯誤,但應用程序部隊關閉,如果我拿出空它給了我錯誤構造函數ArrayWheelAdapter (String [])未定義 – Matt

+0

如果有幫助,可以在這裏查看所有代碼http://code.google.com/p/android-wheel/source/browse/#svn%2Ftrunk%2Fwheel% 2Fsrc%2Fkankan%2Fwheel%2Fwidget%253Fstate%253Dclosed – Matt