下面是FieldActivity.java代码。
我正在学习制作简单的骰子应用。
问题是当我设置" Highorlow" EditText为100或0和
交替按高或低按钮,延迟更新。它是这样的:
High 0.1 - High - 0.2 - High 0.3 - Low 0.4 - Low 0.3 - High - 0.2 - High 0.3.
原因是什么?如何解决这个问题?感谢。
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import java.text.DecimalFormat;
import android.widget.Toast;
public class FieldActivity extends AppCompatActivity {
SharedPreferences mySharedPreferences;
Button Highbtn, Lowbtn;
EditText BetAmount, Highorlow;
TextView Balance;
private static DecimalFormat df1 = new DecimalFormat("0.##");
private static DecimalFormat df2 = new DecimalFormat("0.#");
public double betamount1 = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_field);
Highbtn = (Button) findViewById(R.id.Highbtn);
Lowbtn = (Button) findViewById(R.id.LowBtn);
BetAmount = (EditText) findViewById(R.id.BetAmount);
Highorlow = (EditText) findViewById(R.id.Highorlow);
Balance = (TextView) findViewById(R.id.Balance);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Highbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double multiplier = Double.valueOf(Highorlow.getText().toString());
try {
multiplier = Double.valueOf(Highorlow.getText().toString());
} catch (NumberFormatException e) {
e.printStackTrace();
}
String userData = Balance.getText().toString();
float balance1 = mySharedPreferences.getFloat("FLOAT_KEY1", Float.parseFloat(userData));
if (balance1 >= betamount1) {
SharedPreferences.Editor editor = mySharedPreferences.edit();
double number = Math.random() * 100;
TextView Number = (TextView) findViewById(R.id.Number);
String Number1 = df1.format(number);
String Balance1 = df2.format(balance1);
if (multiplier < number) {
balance1 += 0.1;
editor.putFloat("FLOAT_KEY1", balance1);
editor.apply();
Balance.setText(Balance1);
Balance.setText(String.format("%f", balance1));
Number.setText(Number1);
} if(multiplier > number) {
balance1 -= 0.1;
editor.putFloat("FLOAT_KEY1", balance1);
editor.apply();
Balance.setText(String.format("%f", balance1));
Balance.setText(Balance1);
Number.setText(Number1);
}
}
if (balance1 <= 0.00000000) {
balance1 += 1;
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putFloat("FLOAT_KEY1", balance1);
editor.apply();
Balance.setText(String.format("%f", balance1));
String Balance1 = df2.format(balance1);
Balance.setText(Balance1);
}
if (balance1 < betamount1) {
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putFloat("FLOAT_KEY1", balance1);
editor.apply();
Toast.makeText(getApplicationContext(), "Insufficient Funds", Toast.LENGTH_LONG).show();
}
}
});
Lowbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
float betamount1 = 0;
float multiplier = Float.valueOf(Highorlow.getText().toString());;
try{
multiplier = Float.valueOf(Highorlow.getText().toString());
} catch (NumberFormatException e) {
e.printStackTrace();
}
String userData = Balance.getText().toString();
float balance1 = mySharedPreferences.getFloat("FLOAT_KEY1", Float.parseFloat(userData));
if (balance1 >= betamount1) {
SharedPreferences.Editor editor = mySharedPreferences.edit();
double number = Math.random() * 100;
TextView Number = (TextView) findViewById(R.id.Number);
String Number1 = df1.format(number);
String Balance1 = df2.format(balance1);
if(multiplier > number){
balance1 += 0.1;
editor.putFloat("FLOAT_KEY1", balance1);
editor.apply();
Balance.setText(String.format("%f", balance1));
Balance.setText(Balance1);
Number.setText(Number1);
}
if(multiplier < number){
balance1 -= 0.1;
editor.putFloat("FLOAT_KEY1", balance1);
editor.apply();
Balance.setText(String.format("%f", balance1));
Balance.setText(Balance1);
Number.setText(Number1);
}
}
if (balance1 <= 0.00000000) {
balance1 += 1.00000000;
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putFloat("FLOAT_KEY1", balance1);
editor.apply();
Balance.setText(String.format("%f", balance1));
String Balance1 = df2.format(balance1);
Balance.setText(Balance1);
}
if (balance1 < betamount1) {
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putFloat("FLOAT_KEY1", balance1);
editor.apply();
Toast.makeText(getApplicationContext(), "Insufficient Fund", Toast.LENGTH_LONG).show();
}
}
});
}
}
答案 0 :(得分:0)
而不是使用:
if (balance1 >= betamount1) {
....
}
if (balance1 <= 0.00000000) {
....
}
if (balance1 < betamount1) {
....
}
试
if (balance1 >= betamount1) {
....
} else if (balance1 <= 0.00000000) {
....
} else {
....
}