为什么checkBalance = $ 1200或更多?我的imageView不可见?
我在XML文件中将其设置为不可见。我基本上希望ivArrow ImageView仅在我的checkingBalance超过1199.99美元时才显示。
public class MainActivity extends AppCompatActivity {
ImageView ivArrow;
TextView tvCheckingBalance;
TextView tvInvestmentsBalance;
TextView tvPayRate;
TextView tvToday;
TextView tvWorkBalance;
Button btnWork;
boolean runInitialize = true;
float checkingBalance;
float investmentsBalance;
float payRate;
float workBalance;
int today;
int weekDay;
int workPercent;
DecimalFormat dollarFormat = new DecimalFormat("#,###.00");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences settings = getSharedPreferences("com.vuvaia.lifeofme", MODE_PRIVATE);
runInitialize = settings.getBoolean("runInitialize", runInitialize);
if (runInitialize) {
initialize();
loadSettings();
} else {
loadSettings();
}
// INITIALIZING VIEW SETUP
ivArrow = (ImageView) findViewById(R.id.ivArrow);
tvCheckingBalance = (TextView) findViewById(R.id.tvCheckingBalance);
tvCheckingBalance.setText("$" + dollarFormat.format(checkingBalance));
tvInvestmentsBalance = (TextView) findViewById(R.id.tvInvestmentBalance);
tvInvestmentsBalance.setText("$" + dollarFormat.format(investmentsBalance));
tvPayRate = (TextView) findViewById(R.id.tvPayRate);
tvPayRate.setText("$" + dollarFormat.format(payRate) + "/hr");
tvToday = (TextView) findViewById(R.id.tvToday);
tvToday.setText(String.valueOf(today));
tvWorkBalance = (TextView) findViewById(R.id.tvWorkBalance);
tvWorkBalance.setText("$" + dollarFormat.format(workBalance));
btnWork = (Button) findViewById(R.id.btnWork);
btnWork.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
workButtonPressed();
}
});
// TODO SET UP THIS ARROW THAT TRANSFERS MONEY TO INVESTMENTS
if(checkingBalance > 1199.99f) {
ivArrow.setVisibility(View.VISIBLE);
}
}
@Override
protected void onResume() {
super.onResume();
loadSettings();
}
@Override
protected void onPause() {
super.onPause();
saveSettings();
}
// WORK BUTTON PRESSED ACTIONS **************************************************
public void workButtonPressed(){
if(workPercent < 100){
workBalance += (payRate * .4);
tvWorkBalance.setText("$" + dollarFormat.format(workBalance));
workPercent ++;
advanceDays();
} else {
checkingBalance = checkingBalance + workBalance;
tvCheckingBalance.setText("$" + dollarFormat.format(checkingBalance));
workPercent = 0;
workBalance = 0.0f;
tvWorkBalance.setText("$" + dollarFormat.format(workBalance));
today += 2;
weekDay = 0;
tvToday.setText(String.valueOf(today));
}
}
// WORK BUTTON PRESSED ACTIONS **************************************************
// ADVANCING THE CALENDAR DAYS **************************************************
public void advanceDays(){
if(weekDay <= 17){
weekDay ++;
} else {
weekDay = 0;
today ++;
tvToday.setText(String.valueOf(today));
}
}
// ADVANCING THE CALENDAR DAYS **************************************************
// LOADING AND SAVING **************************************************
public void initialize(){
checkingBalance = 0.00f;
investmentsBalance = 0.00f;
payRate = 7.57f;
today = 0;
weekDay = 0;
workBalance = 0.00f;
workPercent = 0;
saveSettings();
}
public void saveSettings(){
SharedPreferences settings = getSharedPreferences("com.vuvaia.lifeofme", MODE_PRIVATE);
settings.edit().putFloat("checkingBalance", checkingBalance).commit();
settings.edit().putFloat("investmentsBalance", investmentsBalance).commit();
settings.edit().putFloat("payRate", payRate).commit();
settings.edit().putBoolean("runInitialize", false).commit();
settings.edit().putInt("today", today).commit();
settings.edit().putInt("weekDay", weekDay).commit();
settings.edit().putFloat("workBalance", workBalance).commit();
settings.edit().putInt("workPercent", workPercent).commit();
}
public void loadSettings(){
SharedPreferences settings = getSharedPreferences("com.vuvaia.lifeofme", MODE_PRIVATE);
checkingBalance = settings.getFloat("checkingBalance", checkingBalance);
investmentsBalance = settings.getFloat("investmentsBalance", investmentsBalance);
payRate = settings.getFloat("payRate", payRate);
runInitialize = settings.getBoolean("runInitialize", runInitialize);
today = settings.getInt("today", today);
weekDay = settings.getInt("weekDay", weekDay);
workBalance = settings.getFloat("workBalance", workBalance);
workPercent = settings.getInt("workPercent", workPercent);
}
// LOADING AND SAVING **************************************************
}
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EEEEEE"
tools:context="com.vuvaia.lifeofme.MainActivity">
<Button
android:id="@+id/btnWork"
android:layout_width="0dp"
android:layout_height="75dp"
android:background="#2A2A2A"
android:text="Work"
android:textColor="#EEEEEE"
android:textSize="30sp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="32dp" />
<TextView
android:id="@+id/tvWorkBalance"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#2A2A2A"
android:textColor="#EEEEEE"
android:textSize="30sp"
android:textAlignment="center"
android:layout_marginBottom="8dp"
android:paddingBottom="15dp"
android:paddingTop="15dp"
app:layout_constraintBottom_toTopOf="@+id/btnWork"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:id="@+id/tvPayRate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#2A2A2A"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/tvWorkBalance"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:id="@+id/tvCheckingBalance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#2A2A2A"
android:textSize="15sp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="32dp" />
<TextView
android:id="@+id/tvToday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#2A2A2A"
android:textSize="15sp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="32dp"
android:layout_marginLeft="32dp"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:id="@+id/tvInvestmentBalance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#2A2A2A"
android:textSize="15sp"
android:layout_marginRight="32dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="32dp" />
<ImageView
android:id="@+id/ivArrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
app:srcCompat="@drawable/ic_arrow"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/tvCheckingBalance"
app:layout_constraintLeft_toRightOf="@+id/tvCheckingBalance"
android:layout_marginLeft="8dp" />
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:2)
在 btnWork 的 onClick 中添加可见性代码
btnWork.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
workButtonPressed();
// TODO SET UP THIS ARROW THAT TRANSFERS MONEY TO INVESTMENTS
if(checkingBalance > 1199.99f) {
ivArrow.setVisibility(View.VISIBLE);
}
}
});
答案 1 :(得分:1)
我强烈猜你的 checkingBalance 有问题。没有回复你的期望值。
float f1 = 1200;
float f2 = 1199.99f;
int compare = Float.compare(f1,f2);
if(compare > 0){
System.out.println("First is grater");
}else if(compare < 0){
System.out.println("Second is grater");
}else{
System.out.println("Both are equal");
}