2015-10-18 51 views
0

我正在爲學校創建一個咖啡訂購應用程序。我對下面代碼的意圖是根據是否單擊subtractCoffeeButton或addCoffeeButton來增加或減少咖啡整數。 coffeeTV用於向用戶顯示排隊等待排列的咖啡數量。小計和小計電視用於保存價格並將其顯示給用戶。開關語句只增加一次

實際上,subtractCoffee和addCoffee按鈕用於將咖啡和咖啡電視從0增加到1,反之亦然,小計和小計電視也適用於顯示0.00和2.5,但不會增加比這更大的值。當預計將咖啡增加到2,3,4等時,進一步按鈕點擊不會導致任何事情發生。小計爲5.00,7.50,10.00等。

代碼:

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_relative); 

     Button subtractCoffee = (Button) findViewById(R.id.subtractCoffeeButton); 
     subtractCoffee.setOnClickListener(this); 

     Button addCoffee = (Button) findViewById(R.id.addCoffeeButton); 
     addCoffee.setOnClickListener(this); 

} 



    @Override 
    public void onClick(View v) { 
    double subtotal = 0.00; 

    int coffee = 0; 


    double coffeePrice = 2.50; 

    TextView coffeeTV = (TextView) findViewById(R.id.tvCoffeeOrder); 
    String coffeeString = coffeeTV.getText().toString(); 
    int coffeeTracker = Integer.parseInt(coffeeString); 

    TextView subTotalTV = (TextView) findViewById(R.id.tvSubtotalCost); 


    switch (v.getId()) { 
     case R.id.subtractCoffeeButton: 
      if (coffeeTracker == 0) { 
       break; 
      } else if (coffeeTracker == 1) { 
       coffee = 0; 
       coffeeTracker = 0; 
       coffeeTV.setText(Integer.toString(coffee)); 
       break; 
      } else { 
       coffee = coffee - 1; 
       coffeeTV.setText(Integer.toString(coffee)); 
       subtotal = subtotal - coffeePrice; 
       subTotalTV.setText(Double.toString(subtotal)); 
      } 
      break; 
     case R.id.addCoffeeButton: 
      coffee += 1; 
      coffeeTracker+=1; 
      coffeeTV.setText(Integer.toString(coffee)); 
      subtotal = subtotal + coffeePrice; 
      subTotalTV.setText(Double.toString(subtotal)); 
      break; 
    } 

} 
+1

因爲每次調用'onclick','int coffee = 0'都將咖啡設置爲0. – sam

回答

2
@Override 
public void onClick(View v) { 
double subtotal = 0.00; 
int coffee = 0; 
double coffeePrice = 2.50; 

這些變量必須在onClick方法之外。 每次您打電話onClick,他們都會從0開始。

+0

您明白了,謝謝。 –

2

因爲

double subtotal = 0.00; 
int coffee = 0; 
double coffeePrice = 2.50; 

是在方法的局部範圍。聲明爲成員變量,只要當前活動不被破壞,它們的值將一直存在

0

您可以使用其他答案方法(字段變量),或者閱讀並解析每個onClick()上的金額。在代碼中它會看起來像:

static final double COFFEE_PRICE = 2.50; 

@Override 
public void onClick(View v) { 
    TextView coffeeTV = (TextView) findViewById(R.id.tvCoffeeOrder); 
    String coffeeString = coffeeTV.getText().toString(); 
    int coffee = Integer.parseInt(coffeeString); 

    TextView subtotalTV = (TextView) findViewById(R.id.tvSubtotalCost); 
    String subtotalString = subtotalTV.getText().toString(); 
    double subtotal = Double.parseDouble(subtotalString); 

    switch (v.getId()) { 
     case R.id.subtractCoffeeButton: 
      if (coffee == 0) { 
       break; 
      } 
      coffee--; 
      subtotal -= COFFEE_PRICE; 
      coffeeTV.setText(Integer.toString(coffee)); 
      subtotalTV.setText(Double.toString(subtotal)); 
      break; 
     case R.id.addCoffeeButton: 
      coffee++; 
      subtotal += COFFEE_PRICE; 
      coffeeTV.setText(Integer.toString(coffee)); 
      subtotalTV.setText(Double.toString(subtotal));   
      break; 
    } 
} 
0

前面已經說過大部,咖啡和coffePrice需要是的onClick功能之外。此外coffeTracker應始終是相同的咖啡,據我所看到的,所以你不需要變量

這應該是你的代碼:

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 

     double subtotal = 0.00; 
     int coffee = 0; 
     double coffeePrice = 2.50;  

    @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main_relative); 

      Button subtractCoffee = (Button) findViewById(R.id.subtractCoffeeButton); 
      subtractCoffee.setOnClickListener(this); 

      Button addCoffee = (Button) findViewById(R.id.addCoffeeButton); 
      addCoffee.setOnClickListener(this); 

    } 



     @Override 
     public void onClick(View v) { 

     TextView coffeeTV = (TextView) findViewById(R.id.tvCoffeeOrder); 
     TextView subTotalTV = (TextView) findViewById(R.id.tvSubtotalCost); 


     switch (v.getId()) { 
      case R.id.subtractCoffeeButton: 
       if (coffee == 0) { 
        break; 
       } else if (coffee == 1) { 
        coffee = 0; 
        coffeeTV.setText(Integer.toString(coffee)); 
        break; 
       } else { 
        coffee = coffee - 1; 
        coffeeTV.setText(Integer.toString(coffee)); 
        subtotal = subtotal - coffeePrice; 
        subTotalTV.setText(Double.toString(subtotal)); 
       } 
       break; 
      case R.id.addCoffeeButton: 
       coffee += 1; 
       coffeeTV.setText(Integer.toString(coffee)); 
       subtotal = subtotal + coffeePrice; 
       subTotalTV.setText(Double.toString(subtotal)); 
       break; 
     } 

    } 
1

的問題是,你有

double subtotal = 0.00; 
int coffee = 0; 
您的onClick()函數的開始處的

。因此,每次單擊某個按鈕時,都會將該數字重置爲0,然後將其增加到1.

此外,我建議您定義單獨的OnClickListener而不是全局的OnClickListener。例如:

public class MainActivity extends AppCompatActivity { 

    AppWidgetManager appWidgetManager; 
    SharedPreferences preferences; 
    SharedPreferences.Editor editor; 
    InputMethodManager inputMethodManager; 

    EditText mainEditText; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Fabric.with(this, new Crashlytics()); 
     setContentView(R.layout.activity_main); 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      ActivityManager.TaskDescription taskDescription = 
        new ActivityManager.TaskDescription(null, null, getResources().getColor(R.color.primaryDark)); 
      setTaskDescription(taskDescription); 
      getWindow().setNavigationBarColor(getResources().getColor(R.color.primary)); 
     } 

     appWidgetManager = AppWidgetManager.getInstance(this); 
     inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); 

     preferences = PreferenceManager.getDefaultSharedPreferences(this); 
     editor = preferences.edit(); 
     String savedText = preferences.getString("mainText", ""); 

     mainEditText = (EditText) findViewById(R.id.mainEditText); 
     mainEditText.setMovementMethod(new ScrollAndSelectMovingMethod()); 
     mainEditText.getText().append(savedText); 
     Selection.setSelection(mainEditText.getText(), savedText.length()); 

     mainEditText.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       inputMethodManager.showSoftInput(mainEditText, InputMethodManager.SHOW_IMPLICIT); 
      } 
     }); 

     mainEditText.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
      } 

      @Override 
      public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
       onEditTextTextChanged(charSequence.toString()); 
      } 

      @Override 
      public void afterTextChanged(Editable editable) { 
      } 
     }); 
    } 

    @Override 
    public void onBackPressed() { 
     finish(); 
    } 

    private void onEditTextTextChanged(String text) { 
     saveText(text); 
     updateWidget(); 
    } 

    private void saveText(String text) { 
     editor.putString("mainText", text); 
     editor.commit(); 
    } 

    private void updateWidget() { 
     int[] ids = appWidgetManager.getAppWidgetIds(new ComponentName(this, Widget.class)); 
     for (int id : ids) 
      Widget.updateAppWidget(appWidgetManager, id); 
    } 
}