我有一个设置文本,应该更新捐赠的金额。到目前为止,这是所有捐款的总和。在我的捐赠活动中,当按下捐赠按钮时,我会更新一个数字,该数字包含迄今为止的捐赠总额。然后使用getter我从捐赠活动中获取数字到我的主要活动,并将文本设置为数字,但数字不会更新。我有一种感觉,可能是因为我不刷新文字,但我不确定。如果是,那么我不知道如何说,当点击捐赠按钮时,在不同的激活中设置文本。 捐赠活动:
public class DonationAmount extends AppCompatActivity {
public static double donateAmount = 0;
TextView donateAmountView;
Button donateButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_donation_amount);
donateButton = (Button) findViewById(R.id.donate);
donateAmountView = (TextView) findViewById(R.id.donateAmount);
donateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
donateAmount += Double.parseDouble(donateAmountView.getText().toString());
}
});
}
public static double getDonateAmount() {
return donateAmount;
}
}
主要活动
public class MainActivity extends AppCompatActivity {
TextView totalDonations;
private String mercheantKey, userCredentials;
private PaymentParams paymentParams;
private PayuConfig payuConfig;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
totalDonations = (TextView) findViewById(R.id.totalDonation);
final FloatingActionButton donateButton = (FloatingActionButton) findViewById(R.id.fab);
donateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent donateActivity = new Intent(MainActivity.this, DonationAmount.class);
startActivity(donateActivity);
totalDonations.setText(Double.toString(DonationAmount.getDonateAmount()));
}
});
}
答案 0 :(得分:0)
当按钮单击调用时,方法会在调用活动之前输入setText,如下面的代码
donateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
totalDonations.setText(Double.toString(DonationAmount.getDonateAmount()));
Intent donateActivity = new Intent(MainActivity.this, DonationAmount.class);
startActivity(donateActivity);
}
});