2011-11-17 4 views
3

안녕하세요. 휠 픽커가 작동하지만 현재 휠을 위해 0-9에서 번호가 매겨집니다. 나는 0-9 대신에 값을 설정할 수 있기를 원한다. 배열이나 문자열에서 가져온 단어가되고 싶다. 그래서 내가 어디에서 숫자를 빼낼 지 모르겠다. myslef를 입력 할 수있다. 코드는 다음과 같습니다.휠 선택기 용 Android 어레이

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

모든 도움말이 유용합니다. 감사합니다

답변

3

휠 데모 프로젝트를 참조하십시오, 그것은 대단히 바퀴에 문자열 데이터 문제에 관한 도움이 될 것입니다. 아래 코드는 동일한 프로젝트에서 아래.

http://code.google.com/p/android-wheel/

전에서 소스 코드를 가지고있다
public class CitiesActivity extends Activity { 
    // Scrolling flag 
    private boolean scrolling = false; 

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

     setContentView(R.layout.cities_layout); 

     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"}, 
         new String[] {"Paris", "Bordeaux"}, 
         }; 

     final WheelView city = (WheelView) findViewById(R.id.city); 
     city.setVisibleItems(5); 

     country.addChangingListener(new OnWheelChangedListener() { 
         public void onChanged(WheelView wheel, int oldValue, int newValue) { 
          if (!scrolling) { 
           updateCities(city, cities, newValue); 
          } 
         } 
       }); 

     country.addScrollingListener(new OnWheelScrollListener() { 
      public void onScrollingStarted(WheelView wheel) { 
       scrolling = true; 
      } 
      public void onScrollingFinished(WheelView wheel) { 
       scrolling = false; 
       updateCities(city, cities, country.getCurrentItem()); 
      } 
     }); 

     country.setCurrentItem(1); 
    } 

    /** 
    * Updates the city wheel 
    */ 
    private void updateCities(WheelView city, String cities[][], int index) { 
     ArrayWheelAdapter<String> adapter = 
      new ArrayWheelAdapter<String>(this, cities[index]); 
     adapter.setTextSize(18); 
     city.setViewAdapter(adapter); 
     city.setCurrentItem(cities[index].length/2);   
    } 

    /** 
    * Adapter for countries 
    */ 
    private class CountryAdapter extends AbstractWheelTextAdapter { 
     // Countries names 
     private String countries[] = 
      new String[] {"USA", "Canada", "Ukraine", "France"}; 
     // Countries flags 
     private int flags[] = 
      new int[] {R.drawable.usa, R.drawable.canada, R.drawable.ukraine, R.drawable.france}; 

     /** 
     * Constructor 
     */ 
     protected CountryAdapter(Context context) { 
      super(context, R.layout.country_layout, NO_RESOURCE); 

      setItemTextResource(R.id.country_name); 
     } 

     @Override 
     public View getItem(int index, View cachedView, ViewGroup parent) { 
      View view = super.getItem(index, cachedView, parent); 
      ImageView img = (ImageView) view.findViewById(R.id.flag); 
      img.setImageResource(flags[index]); 
      return view; 
     } 

     @Override 
     public int getItemsCount() { 
      return countries.length; 
     } 

     @Override 
     protected CharSequence getItemText(int index) { 
      return countries[index]; 
     } 
    } 
} 
+0

하지만 난 그냥 어딘가에 내가 원하는에서 숫자 0-9를 당기는 순간의 휠 옵션 중 하나에 문자열을 사용하려면 문자열을 사용하려면 – Matt

+0

그 코드를 내가 열거 나 날짜 선택 도구로 사용하려고합니다. 어떻게이 암호를 fucntions와 함께 사용하지만 바퀴에 문자열을 추가하고자 할 때 passwactivity에 추가 할 수 있습니까? 감사합니다 지금까지 – Matt

+0

당신은 AbstractWheelTextAdapter에서 문자열 배열을 사용해야합니다 (답변에서 언급 한 바와 같이) 그리고 문자열은 숫자 어댑터에서 passwactivity에 대한 질문에서 언급 한 바와 같이 사용할 수 없습니다 – Maneesh