从该方法的参数中获取值并显示它
我想从轮的所有4行中获取值,并将值显示为textview或toast。 我的testwheelvalue(,)方法假设从轮子的表面获取值,并将该值返回给我的方法testpin()。在testpin()将值存储到v1-v4之后,方法updatestatus()应该将值的总和显示到文本字段中。从该方法的参数中获取值并显示它
公共类PasswActivity延伸活动{
int testpins;
int v1 = 0;
int v2 = 0;
int v3 = 0;
int v4 = 0;
TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.passw_layout);
initWheelday(R.id.passw_1);
initWheelhour(R.id.passw_2);
initWheelmin(R.id.passw_3);
initWheelsec(R.id.passw_4);
updateStatus();
}
// 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;
updateStatus();
}
};
// Wheel changed listener
private OnWheelChangedListener changedListener = new OnWheelChangedListener() {
public void onChanged(WheelView wheel, int oldValue, int newValue) {
if (!wheelScrolled) {
updateStatus();
}
}
};
/**
* Updates entered PIN status
*/
private void updateStatus() {
text = (TextView) findViewById(R.id.pwd_status);
testPin();
text.setText(String.valueOf(testpins));
// Toast.makeText(getBaseContext(), testpins,
// Toast.LENGTH_SHORT).show();
}
/**
* Initializes wheel
*
* @param id
* the wheel widget Id
*/
private void initWheelsec(int id) {
WheelView wheel = getWheel(id);
wheel.setViewAdapter(new NumericWheelAdapter(this, 0, 59, "%02d"));
wheel.setCurrentItem((int) (Math.random() * 10));
wheel.addChangingListener(changedListener);
wheel.addScrollingListener(scrolledListener);
wheel.setCyclic(false);
wheel.setInterpolator(new AnticipateOvershootInterpolator());
}
private void initWheelmin(int id) {
WheelView wheel = getWheel(id);
wheel.setViewAdapter(new NumericWheelAdapter(this, 0, 59, "%02d"));
wheel.setCurrentItem((int) (Math.random() * 10));
wheel.addChangingListener(changedListener);
wheel.addScrollingListener(scrolledListener);
wheel.setCyclic(false);
wheel.setInterpolator(new AnticipateOvershootInterpolator());
}
private void initWheelhour(int id) {
WheelView wheel = getWheel(id);
wheel.setViewAdapter(new NumericWheelAdapter(this, 0, 23, "%02d"));
wheel.setCurrentItem((int) (Math.random() * 10));
wheel.addChangingListener(changedListener);
wheel.addScrollingListener(scrolledListener);
wheel.setCyclic(false);
wheel.setInterpolator(new AnticipateOvershootInterpolator());
}
private void initWheelday(int id) {
WheelView wheel = getWheel(id);
wheel.setViewAdapter(new NumericWheelAdapter(this, 0, 5, "%02d"));
wheel.setCurrentItem((int) (Math.random() * 10));
wheel.addChangingListener(changedListener);
wheel.addScrollingListener(scrolledListener);
wheel.setCyclic(false);
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 void testPin() {
v1 testWheelValue(R.id.passw_1, v1);
v2 testWheelValue(R.id.passw_2, v2);
v3 testWheelValue(R.id.passw_3, v3);
v4 testWheelValue(R.id.passw_4, v4);
testpins = v1 + v2 + v3 + 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 int testWheelValue(int id, int value) {
int wheel = getWheel(id).getCurrentItem();
wheel = value;
text.setText(String.valueOf(testpins));
return wheel;
}
}
private void testPin() {
v1 testWheelValue(R.id.passw_1, v1);
v2 testWheelValue(R.id.passw_2, v2);
v3 testWheelValue(R.id.passw_3, v3);
v4 testWheelValue(R.id.passw_4, v4);
你缺少以上每个四行的=
,但我认为这只是一个剪切和粘贴错误。
testpins = v1 + v2 + v3 + v4;
我会做return v1 + v2 + v3 + v4;
和摆脱testpins
类成员。
删除该行,因为您正在覆盖正在读取的值。此外,int value
不需要作为参数。
text.setText(String.valueOf(testpins));
摆脱这条线作为updateStatus()
这样做在正确的地方。 text
也是另一个不必要的类成员 - 它应该是updateStatus()
的本地。
return wheel;
}
谢谢,我能够清理我的代码,并使其更容易理解。 – 2012-07-28 17:40:51
你所有的testWheelValue
确实是比较的ID的价值和你v
参数之一,然后返回平等。如果你希望你的v
参数被什么东西所覆盖,你需要告诉程序,以便:
v1 = getWheelValue(R.id.passw_1); // You don't need to pass anything but the ID here
v2 = getWheelValue(R.id.passw_2);
v3 = getWheelValue(R.id.passw_3);
v4 = getWheelValue(R.id.passw_4);
testpins = v1 + v2 + v3 + v4;
updateStatus();
// This method should just use getCurrentItem from the WheelView
private int getWheelValue(int id) {
return getWheel(id).getCurrentItem();
}
这样,v
参数的新值被传递回主代码。
你说得对。我将布尔值的值更改为int,并且我正在玩弄它。我会及时通知你的。 – 2012-07-21 19:55:34
它仍然没有返回0而不是v1,v2,v3和v4的真实值。我更新了我的代码。 – 2012-07-27 02:32:06
这是否也意味着我必须改变我的testpin()方法? – 2012-07-27 02:40:20
除了问题getWheel(int)的代码是什么?我真的不明白你想做什么。我看到你使用'setText()'并假设'text'是某种textView。它不打印你的期望? – yoshi 2012-07-21 18:43:39
它打印出一个0.我希望它打印出v1-v4中所有值的总和。 – 2012-07-21 19:29:32
看看Eric的回答。正如他声称你永远不会改变v1 - v4中的值,并且它们全部用0初始化,所以总和为0.魔术?不,数学^ -^ – yoshi 2012-07-21 19:36:20