Android 字符串数组
大家好,我有下面的代码,它产生四个轮子,每个轮子都有数字 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);
}
}
Hello all i have the code below which produces four wheels each with the numbers 0-9. I think these numbers are being called for each wheel in the section of code after:
/**
* Initializes wheel
* @param id the wheel widget Id
*/
Is there a way i can change this so i can set certain WORDS instead of NUMBERS for each one of the four wheels like and array or string.
So i would have four arrays (strings) with different words for each wheel.
Thanks in advance.
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使用
ArrayWheelAdapter
而不是NumericWheelAdapter
。如果仔细查看自己的代码,您会发现创建适配器的一行。
适配器是轮 UI 可以挂接并抓取数据以显示的东西。这将创建一个包含数字 0 到 9 的适配器。要创建显示单词“Abc”、“Foo”和“Bar”的适配器,请使用此适配器。
Just use an
ArrayWheelAdapter<T>
instead of theNumericWheelAdapter
.If you look closely at your own code, you will find a line where your adapter is created
An adaptor is something the wheel UI can hook onto and grab data to display. This will create an adapter containing the numbers zero through nine. To create an adapter that displays the words "Abc", "Foo", and "Bar" use this.