gpt4 book ai didi

java - 如何在按钮递增时乘以 textview 的值?

转载 作者:行者123 更新时间:2023-11-29 23:09:04 24 4
gpt4 key购买 nike

我目前正在做一个关于 firebase 的项目,我想知道如何在按钮递增时乘以该值。比如我有这张图片

Image

在这张图片中,如果我点击 + 按钮,订单数量将为 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));


}

最佳答案

使用此代码

  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));
}
});

关于java - 如何在按钮递增时乘以 textview 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56205936/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com