2012-07-21 59 views
-1

我想從輪的所有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; 
    } 

} 
+0

除了問題getWheel(int)的代碼是什麼?我真的不明白你想做什麼。我看到你使用'setText()'並假設'text'是某種textView。它不打印你的期望? – yoshi 2012-07-21 18:43:39

+0

它打印出一個0.我希望它打印出v1-v4中所有值的總和。 – 2012-07-21 19:29:32

+2

看看Eric的回答。正如他聲稱你永遠不會改變v1 - v4中的值,並且它們全部用0初始化,所以總和爲0.魔術?不,數學^ -^ – yoshi 2012-07-21 19:36:20

回答

0
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; 
} 
+0

謝謝,我能夠清理我的代碼,並使其更容易理解。 – 2012-07-28 17:40:51

2

你所有的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參數的新值被傳遞迴主代碼。

+0

你說得對。我將布爾值的值更改爲int,並且我正在玩弄它。我會及時通知你的。 – 2012-07-21 19:55:34

+0

它仍然沒有返回0而不是v1,v2,v3和v4的真實值。我更新了我的代碼。 – 2012-07-27 02:32:06

+0

這是否也意味着我必須改變我的testpin()方法? – 2012-07-27 02:40:20

相關問題