简体   繁体   English

将按钮重置为计数并切换选项卡android studio

[英]Reset button to a count and switching tabs android studio

I am making this beer/drink app as a fun project. 我正在将此啤酒/饮料应用程序作为一个有趣的项目。 I checked around the site and couldn't find anything specific to my problem. 我在站点周围进行了检查,找不到与我的问题有关的任何内容。 I would greatly appreciate help at two things; 我非常感谢您在两件事上的帮助;

I would like a button to reset whatever the count gets to in this code, I can't seem to figure out what I need.. 我想要一个按钮来重置此代码中的计数,我似乎无法弄清楚我需要什么。

The countButton is for beer and the drinkButton is for, well yeah, drinks. countButton用于啤酒,drinkBut​​ton用于饮料。

public class Activity2 extends AppCompatActivity {

// Private member field to keep track of the count
private int mCount = 0;
private int mSum = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_2);


    final TextView countTextView = (TextView) findViewById(R.id.TextViewCount);
    final TextView sumTextView = (TextView) findViewById(R.id.textSum);
    final ImageButton countButton = (ImageButton) findViewById(R.id.beerCount);
    final ImageButton drinkButton = (ImageButton) findViewById(R.id.drinkCount);


    countButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            mCount++;
            mSum += 72;
            countTextView.setText("You have been drinking " + mCount + " units!");
            sumTextView.setText("Sum:" + mSum + "!");
        }
    });

    drinkButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            mCount++;
            mSum += 96;
            countTextView.setText("You have been drinking " + mCount + " units!");
            sumTextView.setText("Sum:" + mSum + "!");
        }
    });

Also, the count does remove itself when I toggle through tabs in the app, any ideas to make it stay, and only removing by the reset button? 另外,当我在应用程序中的各个选项卡之间切换时,计数是否会自行删除,是否有任何想法可以保留,仅通过重置按钮即可删除?

Greatly appreciate any help! 非常感谢您的帮助!

For what you require, simply add a third button to your activity and in its onCLickListener, set the values to 0. 根据您的需要,只需在活动中添加第三个按钮,然后在其onCLickListener中将值设置为0。

Something like this. 这样的事情。

resetButton.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        this.mSum = 0;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM