【问题标题】:How do I change variables from different classes?如何更改不同类的变量?
【发布时间】:2010-06-06 02:55:00
【问题描述】:

在深入研究之前,我对 Android 还是很陌生,上个月我才开始学习 Java。我在尝试开发我的第一个简单应用程序时遇到了困难。由于在线随机教程,这些障碍中的大多数都被克服了。我的代码很乱。任何提示都表示赞赏。

上面的问题相当广泛,但这就是我想做的:它本质上是一个血液酒精含量计算器/饮料保持器跟踪器。基本布局:http://i.imgur.com/JGuh7.jpg

底部的按钮只是常规按钮,而不是 ImageButtons(有问题)这是其中的一些示例代码:

<Button android:id="@+id/Button01"
                    android:layout_width="wrap_content"
                    android:layout_marginRight="5dp"
                    android:background="@drawable/addbeer"/>

按钮和TextView都在main.xml中。

我在一个名为 Global.java 的类中定义了变量:

package com.dantoth.drinkingbuddy;

导入android.app.Activity;

公共类全局扩展活动{

public static double StandardDrinks = 0;
public static double BeerOunces = 12;
public static double BeerPercentAlcohol = .05;
public static double BeerDrink = BeerOunces * BeerPercentAlcohol;
public static double BeerDrinkFinal = BeerDrink * 1.6666666;
public static double ShotOunces = 1.5;
public static double ShotPercentAlcohol = .4;
public static double ShotDrink = ShotOunces * ShotPercentAlcohol;
public static double ShotDrinkFinal = ShotDrink * 1.6666666;
public static double WineOunces = 5;
public static double WinePercentAlcohol = .12;
public static double WineDrink = WineOunces * WinePercentAlcohol;
public static double WineDrinkFinal = WineDrink * 1.6666666;
public static double OtherOunces;
public static double OtherPercentAlcohol;
public static double OtherDrink = OtherOunces * (OtherPercentAlcohol * .01);
public static double OtherDrinkFinal = OtherDrink * 1.6666666;
public static double GenderConstant = 7.5; //9 for female
public static double Weight = 180;
public static double TimeDrinking = 60;
public static double Hours = TimeDrinking / 60;
public static double Bac = ((StandardDrinks / 2) * (GenderConstant / Weight)) - (0.017 * Hours);

}

最后一个变量是重要的部分。它会根据所涉及的因素计算您的 BAC。

当我按下添加啤酒按钮 (Button01) 时,我让它向 StandardDrinks 添加 1,模拟喝一瓶啤酒。 Bac 公式中的其他变量在 Global.java 中分配了值。

使啤酒按钮发挥作用的代码在我的常规课程中,drinkbuddy.java:

public class DrinkingBuddy extends Activity {
/** Called when the activity is first created. */


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button button = (Button) findViewById(R.id.Button01);
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Global.StandardDrinks = Global.StandardDrinks + Global.BeerDrinkFinal;
            Toast.makeText(DrinkingBuddy.this, "Mmmm... Beer", Toast.LENGTH_SHORT).show();

        }
        });

根据我的看法,StandardDrinks 现在的值应该为 1。但是,当我单击计算 BAC 按钮 (Button05) 时,它仅输出变量 Bac,就好像 StandardDrinks 仍设置为 0。这是计算的代码BAC 按钮(Button05):

Button button4 = (Button) findViewById(R.id.Button05); button4.setOnClickListener(new OnClickListener() { @覆盖 public void onClick(View v) {

            TextView texty;

            texty = (TextView) findViewById(R.id.texty1);

            texty.setText("Your BAC is " + Global.Bac );

        }
        });

它将以下内容输出到文本视图:“您的 BAC 为 -0.017”。这是如果 StandardDrinks 仍为 0 时的 Bac 值,因此显然类之间的通信存在一些问题。谁能帮帮我??

公式的其他元素(体重、饮酒时间和酒精百分比等)是变量,因为我最终将允许用户在设置中更改这些值。

我在饮水机周围听说全局变量不是好的编程风格,但这是我最接近让它工作的方式。非常欢迎任何其他方式!

【问题讨论】:

    标签: java android variables button global-variables


    【解决方案1】:

    您的程序中有一些逻辑错误。

    public static double Bac = ((StandardDrinks / 2) * (GenderConstant / Weight)) - (0.017 * Hours);
    

    此变量 Bac 将使用公式的值进行初始化。公式中用于计算 BAC 的变量的进一步变化不会反映到它,除非您明确这样做。我建议有一个更新 BAC 的功能,如下所示

    您可以在一个活动中完成以上所有操作。

           public class DrinkingBuddy extends Activity {
    /** Called when the activity is first created. */
    
     double StandardDrinks = 0;
     double BeerOunces = 12;
    double BeerPercentAlcohol = .05;
     double BeerDrink = BeerOunces * BeerPercentAlcohol;
     double BeerDrinkFinal = BeerDrink * 1.6666666;
     double ShotOunces = 1.5;
     double ShotPercentAlcohol = .4;
     double ShotDrink = ShotOunces * ShotPercentAlcohol;
     double ShotDrinkFinal = ShotDrink * 1.6666666;
     double WineOunces = 5;
     double WinePercentAlcohol = .12;
    double WineDrink = WineOunces * WinePercentAlcohol;
    double WineDrinkFinal = WineDrink * 1.6666666;
     double OtherOunces;
     double OtherPercentAlcohol;
     double OtherDrink = OtherOunces * (OtherPercentAlcohol * .01);
     double OtherDrinkFinal = OtherDrink * 1.6666666;
     double GenderConstant = 7.5; //9 for female
     double Weight = 180;
     double TimeDrinking = 60;
     double Hours = TimeDrinking / 60;
     double Bac;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button) findViewById(R.id.Button01);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                StandardDrinks = StandardDrinks + BeerDrinkFinal;
                Toast.makeText(DrinkingBuddy.this, "Mmmm... Beer", Toast.LENGTH_SHORT).show();
    
            }
            });
    
    Button button4 = (Button) findViewById(R.id.Button05); 
    button4.setOnClickListener(new OnClickListener() 
    { 
    @Override 
    public void onClick(View v) {
    
                TextView texty;
                Bac = ((StandardDrinks / 2) * (GenderConstant / Weight)) - (0.017 * Hours);
                texty = (TextView) findViewById(R.id.texty1);
                texty.setText("Your BAC is " + Bac );
    
            }
            });
    

    我的代码很乱。

    确实很乱。我很难删除所有不必要的公共和静态声明。这个document 可能有助于编写Java 代码时遵循的约定。

    根据该文档的引用

    变量应该在哪里初始化 他们被宣布,他们应该是 在最小范围内声明 可能。

    希望它对正确的方向有所帮助。

    【讨论】:

      【解决方案2】:

      关于编码风格和结构可能会有很长的回复,但我会跳过它并继续讨论前面的问题。

      仅仅因为您更新了变量的值,并不意味着使用该变量进行的任何计算都会更新。在这种情况下,更改 StandardDrinks 的值不会导致自动重新计算 Bac。具体来说,分配类静态成员变量的值的代码在类设置期间运行一次,尽管我实际上并不确切知道 dalvik VM 何时执行此操作。重点是每次StandardDrinks 更改值时您需要重新计算Bac。

      【讨论】:

      • 第 1 点是错误的。我认为您的意思是不能更改定义为“最终”的变量。声明“静态”意味着类有一个变量,而不是实例有一个变量。第 2 点是正确的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-03
      • 1970-01-01
      • 2020-02-07
      • 2021-12-06
      • 1970-01-01
      相关资源
      最近更新 更多