Android Studio: Add number every Second

时间秒杀一切 提交于 2019-12-12 05:41:56

问题


I want to display the income of a person in a certain time. For this I ask for the income per month (=gehalt) and working hours per week (stunden) in another activity. Then in the second activity I want to increase the TextView showGehaltproSekunde (id = textViewZahl) every second by the income per second. I am a beginner, so I don't know what exactly I have to write in public void run(). Or is there another possibility to increase the number every second? I hope someone can help me. Thank you!

public class SecondScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.secondlayout);
    Timer t, timer;

    Intent getGehalt = getIntent();
    float gehalt = getGehalt.getFloatExtra("Gehalt", 0);

    Intent getStunden = getIntent();
    float stunden = getStunden.getFloatExtra("Stunden", 0);

    double gehaltProSekunde = gehalt/4/stunden/3600;
    double gehaltProSekundeRounded = Math.round(gehaltProSekunde*1000)/1000.0;

    TextView showGehaltProSekunde = (TextView)findViewById(R.id.textViewZahl);
    showGehaltProSekunde.setText(gehaltProSekundeRounded+" €");

    t = new Timer();
    t.scheduleAtFixedRate( new TimerTask() {
        @Override
        public void run() {

        }
    });
}

}


回答1:


For example analysis this:

private Handler mHandler = new Handler();
private boolean wasRun = true;
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
    if(wasRun){               
       //whatever you want to do if run
       //you can add you want to increase variable here
    }
    mHandler.postDelayed(this, 1000);
}
}, 1000); // 1 seconds



回答2:


Just to give some advice not strictly related to your question (@altan yuksel answered it well), i just wanted to throw my opinion in on your calculations.

You calculate the monthly salary to seconds using salary/4/hours worked in a week/3600(seconds in an hour) however not all months have exactly 4 working weeks (actually only February).

May i suggest asking for yearly salary and performing:
yearly salary / 52 / hours worked in a week / 3600

Or with your current input data:
(monthly salary * 12) / 52 / hours worked in a week / 3600

If you try these to your own you may find them to be a little more accurate.



来源:https://stackoverflow.com/questions/29277367/android-studio-add-number-every-second

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!