我目前正在Firebase上的一个项目上工作,我想知道如何在按钮增加时乘以该值。例如,我有这张图片
在此图像中,如果单击+按钮,则“订单数”将为1,并且它将一直增加,直到单击-按钮为止。在其底部,该值为1.00。例如,每次单击按钮时,它将变为1,它还将乘以1.00的textview。然后,如果将按钮增加,它将是2x1.00,即2.00。
这是我到目前为止所做的代码
public class Detailsoforder extends AppCompatActivity {
private static final String TAG = "AddToDatabase";
private static int click = 0;
private TextView titles;
private TextView increase;
private TextView Price;
private int count = 0;
//add Firebase
private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference myRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detailsoforder);
titles = findViewById(R.id.Order);
increase = findViewById(R.id.Increase);
Price = findViewById(R.id.Price);
String title = getIntent().getStringExtra("title");
String price = getIntent().getStringExtra("description");
titles.setText(title);
Price.setText(price);
mAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
myRef = mFirebaseDatabase.getReference();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
}
};
// Read from the database
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Object value = dataSnapshot.getValue();
Log.d(TAG,"Value is"+value);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
public void onIncreaseClick(View view) {
String price = Price.getText().toString();
int result = Integer.parseInt(price);
count++;
increase.setText(String.valueOf(count));
Price.setText(String.valueOf(count*result));
}
答案 0 :(得分:1)
使用此代码
Button plus = findViewById(R.id.plus);
plus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// for orders numbers
int number = Integer.parseInt(ordersTextView.getText().toString().trim());
number = number+1;
ordersTextView.setText(String.valueOf(number));
// for total cost
Float cost = Float.parseFloat(costTextView.getText().toString().trim())*number;
costTextView.setText(String.valueOf(cost));
}
});
答案 1 :(得分:0)
为此,您不必使用count vaariable,而其他任何操作都可以直接通过文本视图进行,例如本例。
默认情况下,将文本设置为textview 0,然后单击+按钮获取此文本,并在其中添加1,然后再次将值设置为text view
TextView textView = findViewById(R.id.textview);
textView.setText("0");
Button plus = findViewById(R.id.plus);
plus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int number = Integer.parseInt(textView.getText().toString().trim());
number = number+1;
textView.setText(String.valueOf(number));
}
});
现在,您可以通过这种方式为文本视图设置增量值,请检查它是否可以工作,然后询问我是否需要其他帮助。