【发布时间】: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