简体   繁体   English

Android Studio:如何使用 Textview 始终显示更新变量?

[英]Android studio: How do I show an updating variable at all times using a Textview?

在此处输入图像描述
The android studio code works as intended. android studio 代码按预期工作。 However, points are made in the background since it's an idle game.但是,由于这是一款闲置游戏,因此在后台进行积分。 An on click method only shows the points that were made as of the last click.单击方法仅显示上次单击时所做的点。 I need the points to be shown at all times.我需要始终显示这些点。

I have been testing this new added bit of code but the points are not updating when the button is clicked.我一直在测试这个新添加的代码,但是当点击按钮时这些点没有更新。 Example in photo.照片中的示例。 Evolution Points = 0;进化点=0; The on click method is updating but there are also points made in the background that need to be shown without a button click. on click 方法正在更新,但在背景中也有一些点需要在没有按钮单击的情况下显示。 Any suggestions?有什么建议么?

visibletotals.setText("Evolutions Points: " + clicks);

full code for reference完整代码供参考

package com.example.idleclicker;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    import java.util.Timer;
    import java.util.TimerTask;
    
    
    public class MainActivity extends AppCompatActivity {
    
        TextView points;
    TextView visibletotals;
        Button click;
        Button upgradebtn;
        TextView Leveltext;
    int clicks = 0;
    int clickcost = 10;
    int upgradelevel = 1;
        Timer myTimer = new Timer();
    
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            click = (Button) findViewById(R.id.click);
            points = (TextView) findViewById(R.id.points);
            upgradebtn = (Button) findViewById(R.id.upgradebtn);
            Leveltext = (TextView) findViewById(R.id.leveltext);
            visibletotals = (TextView) findViewById(R.id.visibletotals);
    
    
            visibletotals.setText("Evolutions Points: " + clicks);
    
    
            click.setEnabled(true);
    
            upgradebtn.setEnabled(true);
    
            myTimer.schedule(new TimerTask() {
                                 @Override
                                 public void run() {
                                     clicks+= upgradelevel;
    
                                 }
                             }, 0, 1000);
    
    
    
            click.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            clicks++;
                            points.setText(getResources().getString(R.string.evol) + clicks);
    
                        }
    
    
                    });
    
            upgradebtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (clicks >= clickcost) {
    
                        clicks -= clickcost;
    
                        upgradelevel += 1;
    
                        clickcost *= 2;
                        upgradebtn.setText(getResources().getString(R.string.Upgrade) +
                                clickcost +
                                getString(R.string.LevelText)
                                + upgradelevel);
    
                    };
                }
    
                });
    
    
        }}

I can see that your clicks is changing(dynamic) while your set text is static that means it is only replying at the start and it is not triggered from the next time you do it.我可以看到您的点击正在改变(动态),而您的设置文本是静态的,这意味着它只是在开始时回复,并且不会在您下次执行时触发。 one way to tackle this is put the settext value in the trigger block(ie onclicklistener) and make it set the fresh text as per triggered.解决此问题的一种方法是将 settext 值放入触发块(即 onclicklistener)并使其根据触发设置新文本。

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

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