2013-03-15 64 views
0

我,新的android可以任何人幫助我這個簡單的,但我不能識別這個執行process.I需要動態結果通過微調,但這裏微調執行while在運行時只(不點擊微調) 不好意思問這個問題不能即時解決這個... :(Android:通過微調器動態獲取結果

protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.loanpayment); 
    try { 
     et1 = (EditText) findViewById(R.id.et1); 
     et2 = (EditText) findViewById(R.id.et2); 
     et3 = (EditText) findViewById(R.id.et3); 
     ans = (TextView) findViewById(R.id.editText1); 
     b1 = (Button) findViewById(R.id.button1); 
     b2 = (Button) findViewById(R.id.button2); 
     s = (Spinner) findViewById(R.id.spin); 


    } 
    catch (Exception e) { 
     // TODO: handle exception 
     e.printStackTrace(); 
    } 
    ArrayAdapter<String> a = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, values); 
    a.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    s.setAdapter(a); 

    s.setOnItemSelectedListener(new OnItemSelectedListener() { 


     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, 
       int position, long id) { 

      // TODO Auto-generated method stub 
      String item = parent.getItemAtPosition(position).toString(); 
      if(item.equals("Yearly")) { 
       Pattern p = Pattern.compile("([0-9 .]*)"); 

       if (et1.getText().toString().trim().length() > 0 && et2.getText().toString().trim().length() > 0 && et3.getText().toString().trim().length() > 0) { 
        Matcher m = p.matcher(et1.getText().toString().trim()); 
        Matcher m1 = p.matcher(et2.getText().toString().trim()); 
        Matcher m2 = p.matcher(et3.getText().toString().trim()); 

        if (m.matches() && m1.matches() && m2.matches()) { 

         amount = Float.valueOf(et1.getText().toString()); 
         interest = Float.valueOf(et2.getText().toString()); 
         year = Float.valueOf(et3.getText().toString()); 
         s1 = interest/100; 
         s2 = year; 
         ans1 = s1 * amount; 
         ans2 = (float) (1-Math.pow((1+s1), -s2)); 
         tot = (float) Math.round(ans1/ans2); 
         String an = Float.toString(tot); 
         ans.setText("Amount to Pay=" +an); 

        } 
        else { 
         Toast.makeText(getApplicationContext(), 
           "Incorrect value", Toast.LENGTH_SHORT) 
           .show(); 
        } 
+0

我很困惑。你得到了什麼結果,你想要什麼結果? – codeMagic 2013-03-15 15:07:14

+0

@ codeMagic--在程序中,我希望通過微調器動態生成結果,所以我在微調onItemSelected中編寫了程序。所以,在運行時它會執行微調而不考慮其他字段 – user2174439 2013-03-15 15:11:59

+0

@codeMagic你能否用你的澄清解釋來更新你的問題? – rhughes 2013-03-15 15:17:18

回答

0

如果我理解正確的話,應該This SO Answer解決您的問題

由於@Sam狀態在評論那裏

...紡紗廠調用onItemSelected()時,它們加載默認值 。

所以,我的理解是,當Spinner被加載,所以你需要使用一些標誌,讓該函數知道它最初加載並跳過邏輯onItemSelected()將被調用。然後,您可以更改標記,以便在用戶選擇項目後,該功能將繼續執行邏輯。希望這可以幫助。

注意

我還沒有發現在這個文檔中關於這個事情,但我已經通過功能加強,以驗證這種情況的發生。

0

只要您設置spinner.setOnItemClickListener它一旦得到儘快佈局奠定叫,你必須處理它手動設置一些標誌

+0

我們不能動態調用它嗎? – user2174439 2013-03-15 15:33:46