在深入研究之前,我是 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)类全局扩展 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 中分配了值。
让啤酒按钮起作用的代码在我的常规类(class) drinkingbuddy.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):
按钮 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 );
}
});
它向 TextView 输出以下内容:“您的 BAC 是 -0.017”。如果 StandardDrinks 仍为 0,则这是 Bac 值,因此很明显类之间的通信存在一些问题。谁能帮帮我??
公式的其他元素(体重、饮酒时间和酒精百分比等)是变量,因为我最终将允许用户在设置中更改这些值。
我听说过全局变量不是好的编程风格,但这是我最接近它的工作方式。非常欢迎任何其他方式!
最佳答案
您的程序中存在一些逻辑错误。
public static double Bac = ((StandardDrinks / 2) * (GenderConstant / Weight)) - (0.017 * Hours);
此变量 Bac 将使用公式的值进行初始化。除非您明确这样做,否则用于计算 BAC 的公式中使用的变量的进一步变化不会反射(reflect)回它。我建议有一个函数来更新 BAC,如下所示
您可以在一个 Activity 中完成上述所有操作。
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 );
}
});
MY CODE IS VERY MESSY.
确实很乱。我很难删除所有不必要的公共(public)和静态声明。这document可能有助于在编写 Java 代码时遵循约定。
根据该文档引用
Variables should be initialized where they are declared and they should be declared in the smallest scope possible.
希望它对正确的方向有所帮助。
关于java - 如何更改来自不同类的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2982940/