Android String Array - PullRequest
       2

Android String Array

0 голосов
/ 18 ноября 2011

Привет всем, у меня есть код ниже, который производит четыре колеса с номерами 0-9. Я думаю, что эти цифры вызываются для каждого колеса в разделе кода после:

/**
* Initializes wheel
* @param id the wheel widget Id
*/

Есть ли способ, которым я могу изменить это, чтобы я мог установить определенные СЛОВА вместо НОМЕРОВ для каждого из четырех колес, таких как и массив или строка.

Таким образом, у меня было бы четыре массива (строки) с разными словами для каждого колеса.

Спасибо заранее.

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

}

1 Ответ

1 голос
/ 18 ноября 2011

Просто используйте ArrayWheelAdapter<T> вместо NumericWheelAdapter.

Если вы внимательно посмотрите на свой собственный код, вы найдете строку, где создан ваш адаптер

new NumericWheelAdapter(this, 0, 9)

Адаптер - это то, что пользовательский интерфейс колеса может привязать и получить данные для отображения. Это создаст адаптер, содержащий цифры от нуля до девяти. Чтобы создать адаптер, который отображает слова «Abc», «Foo» и «Bar», используйте это.

new ArrayWheelAdapter<String>(this, new String[]{"Abc", "Foo", "Bar"})
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...